function Parent() { this.name = 'zhangsan'; this.children = ['A', 'B', 'C']; } Parent.prototype.getName = function() { console.log(this.name); } function Child() { } Child.prototype = new Parent(); var child = new Child(); console.log(child.getName());
[!NOTE]
主要问题:javascript
function Parent(age) { this.names = ['zhangsan', 'lisi']; this.age = age; this.getName = function() { return this.names; } this.getAge = function() { return this.age; } } function Child(age) { Parent.call(this, age); } var child = new Child(18); child.names.push('haha'); console.log(child.names); var child2 = new Child(20); child2.names.push('yaya'); console.log(child2.names);
[!NOTE]
优势:java
[!DANGER]
缺点:函数
/** * 父类构造函数 * @param name * @constructor */ function Parent(name) { this.name = name; this.colors = ['red', 'green', 'blue']; } Parent.prototype.getName = function() { console.log(this.name); } // child function Child(name, age) { Parent.call(this, name); this.age = age; } Child.prototype = new Parent(); // 校订child的构造函数 Child.prototype.constructor = Child; // 建立实例 var child1 = new Child('zhangsan', 18); child1.colors.push('orange'); console.log(child1.name, child1.age, child1.colors); // zhangsan 18 (4) ["red", "green", "blue", "orange"] var child2 = new Child('lisi', 28); console.log(child2.name, child2.age, child2.colors); // lisi 28 (3) ["red", "green", "blue"]
[!NOTE]
优势: 融合了原型链继承和构造函数的优势,是Javascript中最经常使用的继承模式优化
function createObj(o) { function F(){}; // 关键:将传入的对象做为建立对象的原型 F.prototype = o; return new F(); } // test var person = { name: 'zhangsan', friends: ['lisi', 'wangwu'] } var person1 = createObj(person); var person2 = createObj(person); person1.name = 'wangdachui'; console.log(person1.name, person2.name); // wangdachui, zhangsan person1.friends.push('songxiaobao'); console.log(person2.friends); // lisi wangwu songxiaobao
[!DANGER]
缺点:ui
// 建立一个用于封装继承过程的函数,这个函数在内部以某种形式来加强对象 function createObj(o) { var clone = Object.create(o); clone.sayName = function() { console.log('say HelloWorld'); } return clone; }
[!DANGER]
缺点:与借用构造函数模式同样,每次建立对象都会建立一遍方法this
function Parent(name) { this.name = name; this.colors = ['red', 'green', 'blue']; } Parent.prototype.getName = function() { console.log(this, name); } function Child(name, age) { Parent.call(this, name); this.age = age; } // test1: // 1. 设置子类实例的时候会调用父类的构造函数 Child.prototype = new Parent(); // 2. 建立子类实例的时候也会调用父类的构造函数 var child1 = new Child('zhangsan', 18); // Parent.call(this, name); // 思考:如何减小父类构造函数的调用次数呢? var F = function(){}; F.prototype = Parent.prototype; Child.prototype = new F(); // 思考:下面的这一句话能够吗? /* 分析:由于此时Child.prototype和Parent.prototype此时指向的是同一个对象, 所以部分数据至关于此时是共享的(引用)。 好比此时增长 Child.prototype.testProp = 1; 同时会影响 Parent.prototype 的属性的。 若是不模拟,直接上 es5 的话应该是下面这样吧 Child.prototype = Object.create(Parent.prototype);*/ Child.prototype = Parent.prototype; // 上面的三句话能够简化为下面的一句话 Child.prototype = Object.create(Parent.prototype); // test2: var child2 = new Child('lisi', 24);
// 自封装一个继承的方法 function object(o) { // 下面的三句话实际上就是相似于:var o = Object.create(o.prototype) function F(){}; F.prototype = o.prototype; return new F(); } function prototype(child, parent) { var prototype = object(parent.prototype); // 维护原型对象prototype里面的constructor属性 prototype.constructor = child; child.prototype = prototype; } // 调用的时候 prototype(Child, Parent)
var o1 = {name: 'value'}; var o2 = new Object({name: 'value'}); var M = function() {this.name = 'o3'}; var o3 = new M(); var P = {name: 'o4'}; var o4 = Object.create(P)
__proto__
内部属性,这个属性所对应的就是该对象的原型__proto__
以外,还预置了 prototype 属性__proto__
。任何一个实例对象经过原型链能够找到它对应的原型对象,原型对象上面的实例和方法都是实例所共享的。es5
一个对象在查找以一个方法或属性时,他会先在本身的对象上去找,找不到时,他会沿着原型链依次向上查找。prototype
[!NOTE]
注意: 函数才有prototype,实例对象只有有__proto__, 而函数有的__proto__是由于函数是Function的实例对象指针
[!NOTE]
判断实例对象的__proto__属性与构造函数的prototype是否是用一个引用。若是不是,他会沿着对象的__proto__向上查找的,直到顶端Object。code
[!NOTE]
使用对象.construcor
直接可判断
var obj = {}; obj.__proto__ = Base.prototype; Base.call(obj);
// 普通写法 function Animal() { this.name = 'name' } // ES6 class Animal2 { constructor () { this.name = 'name'; } }