这是一些代码
var Animal = function (name,color,sound){
this.name = name;
this.color = color;
this.sound = sound;
}
Animal.prototype.doSomething = function (){
alert(this.sound);
}
var cate = new Animal('cate','black','meow');
cat.doSomething(); \\alerts 'meow'
now i create another constructor function
var Person = function (name){
this.name = name;
}
下面我用 Animal.prototype 初始化了一个 Person.prototype
Person.prototype = Animal.prototype;
所以现在我们知道对象被分配为引用Person.prototype = Animal.prototype
将分配Animal.prototype
给Person.prototype
所以如果我们添加到Person.prototype
像这样的方法
Person.prototype.doSomethingElse = function (){
alert("some text to test");
}
Animal.Prototype 是否也得到doSomethingElse
了Person.prototype
.
cate.doSomethingElse();