JavaScript组合继承

102 阅读1分钟
function Parent(name){
    this.name = name;
    this.color = ['red', 'green', 'yellow']
}

function Child(name, age){
    Parent.call(this, name);
    this.age = age;
}

Child.prototype = new Parent();
var child1 = new Child('susan', 18);
var child2 = new Child('david', 19);
child1.color.push('blue');
child1.color.length // 4;
child2.color.length // 3;