【发布时间】:2013-03-15 04:29:59
【问题描述】:
这是一些代码
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 是否也获得了Person.prototype 的doSomethingElse 方法。
cate.doSomethingElse();
【问题讨论】:
-
不确定。试一试,找出答案! jsfiddle.net/bryanjamesross/d4g3a
-
是的,确实如此。是这个问题吗?
-
是的,这是好事吗?
-
抱歉,这里没有看到问题。