原型链继承
继承的实现
//继承实现:将子类的原型指向父类的实例
Child.prototype = new Parent();
Child.prototype.constructor = Child;
缺点
子类实例修改属性,会相互影响
代码
function Parent() {
this.name = 'parent';
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function () {
console.log(this.name);
}
function Child() {
}
//继承实现:将子类的原型指向父类的实例
Child.prototype = new Parent();
Child.prototype.constructor = Child;
var child1 = new Child();
var child2 = new Child();
console.log(child1.colors); //["red", "blue", "green"]
child1.getName(); //parent
child2.