阿里巴巴技术文章分享 Javascript继承机制的实现(2)

People.prototype.summary = function() { return this.firstname + " " + this.lastname; }; Employee.prototype.summary = function() { return this.super.call(this) + ", " + this.company; }; Manager.prototype.summary = function() { return this.super.call(this) + ", " + this.title; };

在所有的成员方法都已经定义好之后,声明类的继承(必须先定义方法,再声明类的继承,否则无法在方法中使用this.super调用父类方法!):

extend(People, Employee); extend(Employee, Manager);

使用这些类就比较简单,直接new就好了:

var people = new People("Alice", "Dickens"); var employee = new Employee("Bob", "Ray", "Alibaba"); var manager = new Manager("Calvin", "Klein", "Alibaba", "Senior Manager"); console.log( people.summary() ); //Alice Dickens console.log( employee.summary() ); //Bob Ray, Alibaba console.log( manager.summary() ); //Calvin Klein, Alibaba, Senior Manager

这篇文章不错吧,那就给个赞吧!

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwdsd.html