一、原型链继承
Parent.prototype.grand = 'grand';
function Parent (name) {
this.name = name || 'parent';
this.color = ['red', 'orange', 'yellow'];
}
function Child () {
}
Child.prototype = new Parent();
var child1 = new Child('child1');
var child2 = new Child('child2');
缺点
1.引用值共享问题
引用值会被所有的实例(这里以两个为例,下面也将都是)共享,一个对象修改了原型属性,那么另一个的原型属性也会被修改
2.不能传参
在创建Child的实例时,不能向Parent传递参数;如果传递也不会有作用
二、借用构造函数(经典继承)
Parent.prototype.grand = 'grand';
function Parent (name) {
this.name = name || 'parent';
this.color = ['red', 'orange', 'yellow'];
}
function Child (name,) {
Parent.call(this, name);
}
var child1 = new Ch