function Parent() {
this.name = 'parent';
this.colors = ['black', 'yellow', 'red']
}
function Child() {
this.type = 'child';
}
Parent.prototype.age = 12;
Parent.prototype.say = function(){
console.log('hello');
}
Child.prototype = new Parent();
var q1 = new Child();
console.log(q1);
console.log(q1.age); // 12
console.log(q1.say()); // hello undefined(因为方法没返回))
var q2 = new Child();//具有相同的原型,所以不分彼此
q1.colors.push('pink');
console.log(q1.colors) // [ 'black', 'yellow', 'red', 'pink' ]
console.log(q2.colors) // [ 'black', 'yellow', 'red', 'pink' ]
06-15
554
01-23
2658
09-19
1189