Javascript Object.create未在函数中工作
这是我的密码:Javascript Object.create未在函数中工作,javascript,Javascript,这是我的密码: function Mammal(name){ this.name = name; this.offspring = []; } Mammal.prototype.sayHello = function(){ return "My name is " + this.name + ", I'm a " + this.constructor.name; } function Cat(name, color){ Mammal.call(this,
function Mammal(name){
this.name = name;
this.offspring = [];
}
Mammal.prototype.sayHello = function(){
return "My name is " + this.name + ", I'm a " + this.constructor.name;
}
function Cat(name, color){
Mammal.call(this, name);
this.color = color;
}
现在,当我使用此函数调用Object.create from时:
function extendWithObjectCreate(child, parent) {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = child;
}
Object.create不返回链接到父原型的对象
你能在函数中使用Object.create吗?当我这样做时,结果很好:
功能哺乳动物(名称){
this.name=名称;
this.founders=[];
}
Madama.prototype.sayHello=函数(){
返回“我的名字是”+this.name+”,我是“+this.constructor.name”;
}
功能类别(名称、颜色){
哺乳动物。叫(这个,名字);
这个颜色=颜色;
}
函数extendWithObjectCreate(子、父){
child.prototype=Object.create(parent.prototype);
child.prototype.constructor=child;
}
extendWithObjectCreate(猫、哺乳动物);
警惕(新猫('Muffin','blue')。sayHello())代码>当我这样做时,结果很好:
功能哺乳动物(名称){
this.name=名称;
this.founders=[];
}
Madama.prototype.sayHello=函数(){
返回“我的名字是”+this.name+”,我是“+this.constructor.name”;
}
功能类别(名称、颜色){
哺乳动物。叫(这个,名字);
这个颜色=颜色;
}
函数extendWithObjectCreate(子、父){
child.prototype=Object.create(parent.prototype);
child.prototype.constructor=child;
}
extendWithObjectCreate(猫、哺乳动物);
警惕(新猫('Muffin','blue')。sayHello())
你能解释一下你是如何调用extendWithObjectCreate
的吗?你如何将child
和parent
传递给extendWithObjectCreate
?extendWithObjectCreate(child,哺乳类)为什么你认为“Object.create不返回链接到父原型的对象?”?你观察到了什么?您希望找不到的链接是什么?您能解释一下如何调用extendWithObjectCreate
?如何将child
和parent
传递到extendWithObjectCreate
?extendWithObjectCreate(child,哺乳类)为什么您认为“Object.create不返回链接到父原型的对象。”? 你观察到了什么?您希望找不到的链接是什么?您能解释一下如何调用extendWithObjectCreate
?如何将child
和parent
传递到extendWithObjectCreate
?extendWithObjectCreate(child,哺乳类)为什么您认为“Object.create不返回链接到父原型的对象。”? 你观察到了什么?你以为你没有找到什么链接?@qantaschrome也有对象。hasOwnProperty
。啊,我错了,Firefox没有。这并不像你想象的那样,因为它会在对象上调用它,而不是Cat.prototype
。啊。那是我的错。谢谢你的修复。@Qantas Chrome也有对象。hasOwnProperty
。啊,我错了,Firefox没有。这并不像你想象的那样,因为它会在对象上调用它,而不是Cat.prototype
。啊。那是我的错。谢谢你的修复。@Qantas Chrome也有对象。hasOwnProperty
。啊,我错了,Firefox没有。这并不像你想象的那样,因为它会在对象上调用它,而不是Cat.prototype
。啊。那是我的错。谢谢你把它修好。