function Parent () {
this.name = 'kevin';
}
Parent.prototype.getName = function () {
console.log(this.name);
}
function Child () {
}
Child.prototype = new Parent();
var child1 = new Child();
console.log(child1.getName()) // kevin
复制代码
缺点bash
function Parent () {
this.names = ['kevin', 'daisy'];
}
function Child () {
}
Child.prototype = new Parent();
var child1 = new Child();
child1.names.push('yayu');
console.log(child1.names); // ["kevin", "daisy", "yayu"]
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy", "yayu"]
复制代码
function Parent () {
this.names = ['kevin', 'daisy'];
}
function Child () {
Parent.call(this);
}
var child1 = new Child();
child1.names.push('yayu');
console.log(child1.names); // ["kevin", "daisy", "yayu"]
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy"]
复制代码
优势函数
function Parent (name) {
this.name = name;
}
function Child (name) {
Parent.call(this, name);
}
var child1 = new Child('kevin');
console.log(child1.name); //kevin
var child2 = new Child('daisy');
console.log(child2.name); // daisy
复制代码
缺点ui
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child (name, age) {
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
var child1 = new Child('kevin', '18');
child1.colors.push('black');
console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]
var child2 = new Child('daisy', '20');
console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]
复制代码
优势this
function createObj (o) {
function F () {}
F.prototype = o;
return new F();
}
复制代码
就是ES5 Object.create的模拟实现,将传入的对象做为做为建立的对象的原型。spa
缺点prototype
包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承同样。code
var person = {
name: 'kevin',
friends: ['daisy', 'kelly']
}
var person1 = createObj(person);
var person2 = createObj(person);
person1.name = 'person1';
console.log(person2.name); // kevin
person1.friends.push('taylor');
console.log(person2.friends); // ["daisy", "kelly", "taylor"]
复制代码
注意: 修改person1.name的值,person2.name的值并未发生改变,并非由于person1和person2有独立的name值,而是由于person1.name = 'person1',给person1添加了name值,并未修改了原型上的name值。对象
建立一个仅用于封装继承过程的函数,该函数在内部以某种形式来作加强对象,最后返回对象继承
function createObj (o) {
var clone = object.create(o);
clone.sayName = function () {
console.log('hi');
}
return clone;
}
复制代码
缺点 跟借用构造函数模式同样,每次建立对象都会建立一遍方法。seo
为了方便你们阅读,在这里重复一下组合继承的代码:
function Parent (name) {
this.name = name;
this.colors = ["red", "blue", "green"];
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child (name, age) {
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
var child1 = new Child('kevin', '18');
console.log(child1)
复制代码
组合继承最大的缺点是会调用两次父构造函数。
一次是设置子类型实例的原型的时候:
Child.prototype = new Parent();
复制代码
一次在建立子类型实例的时候:
var child1 = new Child('kevin', '18');
复制代码
回想下new的模拟实现,其实在这句中,咱们会执行:
Parent.call(this, name);
复制代码
在这里,咱们又会调用了一次Parent构造函数。
因此,在这个例子中,若是咱们打印child1对象,咱们会发现Child.prototype和child1都有一个属性为color,属性值为["red", "blue", "green"]。
那么咱们该如何避免这一次重复调用呢?
若是咱们不使用Child.prototype = new Parent(),而是直接的让Child.prototype访问到Parent.prototype呢? 👇
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child (name, age) {
Parent.call(this, name);
this.age = age;
}
//关键的第3步
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
var child1 = new Child('kevin', '18');
console.log(child1);
复制代码
最后咱们封装一下这个继承方法:
function object (o) {
function F () {}
F.prototype = o;
return new F();
}
function prototype(child, parent) {
var prototype = object(parent.prototype);
prototype.constructor = child;
child.prototype = prototype;
}
//使用
prototype(Child, Parent);
复制代码
优势