摘要:
原型链 假借构造函数 组合继承 原型式(Prototypal) 寄生式 组合寄生式 主要形式 subType.prototype = new SuperType() SuperType.call(this) subType.prototype = new SuperType() + SuperTyp 阅读全文
摘要:
普通的组合继承模式中,我们两次调用了父类的构造函数,导致父类的实例属性在子类实例和子类原型上同时存在。主要问题在于在第一次调用时,我们将一个父类实例作为子类的原型,使得父类的实例属性不必要的被复制到了子类原型上。 理想的效果是,只将子类原型的[[Prototype]]隐式属性指向父类原型,不在原型上 阅读全文
摘要:
基本模式 原型式继承(Prototypal Inheritance)和工厂模式的结合,用一个函数先创建以父类为原型的对象,再为这个对象添加需要的子类属性及方法。最后直接返回这个对象。 function object(o){ function F(){} F.prototype = o; return 阅读全文
摘要:
基本形式 //创建一个以o为原型的对象 function object(o){ function F(){} F.prototype = o; return new F(); } let person = { name: "Jack", friends: ["Ann", "Bob"] }; let 阅读全文
摘要:
基本形式 通过原型链继承原型属性及方法,通过假借构造函数继承实例属性及方法。 function SuperType(name){ this.name = name; this.colors = ["red", "blue"]; } SuperType.prototype.sayName = func 阅读全文
摘要:
基本形式 function Person(name){ this.name = name; } Person.prototype.sayHi = function(){ console.log(this.name); } function Student(name, mark){ Person.ca 阅读全文
摘要:
原型链实现继承 基本模式 function SuperType(){ this.property = true; } SuperType.prototype.getSuperValue = function(){ console.log(this); return this.property; } 阅读全文