从我的理解中,Prototype对象是其他对象继承属性和方法的对象,基本上它拥有一个构造器属性,指向或指向构造函数创建Object.Please考虑下面的代码:结合使用JS的原型和构造器属性
function Animal()
{
this.name="no name";
}
function Cat()
{
Animal.Call(this); //Please Explain
this.mood="sleepy";
}
Cat.prototype=new Animal(); //Cat inheriting Animal?
Cat.prototype.constructor=Cat; //Please Explain
请解释清楚,但在细节的代码行意见和反映的概念,谢谢。
你可能是指'Animal.call(this);'。你究竟想要解释什么?什么'.call'呢?为什么它在那里?或者是什么?顺便说一句,更好的建立原型是'Cat.prototype = Object.create(Animal.prototype);'。在那时你的代码中,你不想创建一个新的'Animal'实例。 – 2013-02-22 13:59:08
是的,我想知道'Animal.call(this)'和'Cat.prototype.constructor = Cat'的用途是什么,'谢谢。 – MrShabana 2013-02-22 14:05:01