继承的类没有在Javascript中获得自己的方法
这是未定义的。为什么?这应该说明,因为它是一种分类有机体的方法。 执行以下操作时,打印()不会显示在对象中:继承的类没有在Javascript中获得自己的方法,javascript,inheritance,Javascript,Inheritance,这是未定义的。为什么?这应该说明,因为它是一种分类有机体的方法。 执行以下操作时,打印()不会显示在对象中: function Mammal(name){ this.name = name; } Mammal.prototype.displayName = function(){ return this.name; } function Organism(name){ this.orgName = name; } Organism.prototype.print = func
function Mammal(name){
this.name = name;
}
Mammal.prototype.displayName = function(){
return this.name;
}
function Organism(name){
this.orgName = name;
}
Organism.prototype.print = function(){
return this.orgName;
}
Organism.prototype = new Mammal(); //Organism inherits Mammal
//Testing
var o = new Organism('Human');
o.print()
替换整个prototype
对象,从而删除先前指定的:
Organism.prototype = new Mammal(); //Organism inherits Mammal
您可以通过更改顺序来修复它,以便将新方法“添加”到继承的原型中:
Organism.prototype.print = function(){
return this.orgName;
}
顺便提一下,您应该考虑使用
organic.prototype=Object.create(哺乳动物.prototype)而应该调用基对象的构造函数。有关示例,请参见。当您执行以下操作时:
function Mammal(name){
this.name = name;
}
Mammal.prototype.displayName = function(){
return this.name;
}
function Organism(name){
this.orgName = name;
}
Organism.prototype.print = function(){
return this.orgName;
}
Organism.prototype = new Mammal(); //Organism inherits Mammal
//Testing
var o = new Organism('Human');
o.print()
替换整个prototype
对象,从而删除先前指定的:
Organism.prototype = new Mammal(); //Organism inherits Mammal
您可以通过更改顺序来修复它,以便将新方法“添加”到继承的原型中:
Organism.prototype.print = function(){
return this.orgName;
}
顺便提一下,您应该考虑使用organic.prototype=Object.create(哺乳动物.prototype)而应该调用基对象的构造函数。有关示例,请参见。当您分配
function Organism(name){
this.orgName = name;
}
Organism.prototype = new Mammal(); //Organism inherits Mammal
Organism.prototype.print = function(){
return this.orgName;
}
您正在撞击具有打印功能的Organic.prototype对象。为您的继承尝试以下方法:
Organism.prototype = new Mammal();
当你分配
function Organism(name){
this.orgName = name;
}
Organism.prototype = new Mammal(); //Organism inherits Mammal
Organism.prototype.print = function(){
return this.orgName;
}
您正在撞击具有打印功能的Organic.prototype对象。为您的继承尝试以下方法:
Organism.prototype = new Mammal();
“这是未定义的。”无法重新编程,我得到错误未捕获类型错误:未定义不是函数。另外,还可以了解如何正确设置继承。“这是未定义的。”无法重新编程,我得到错误未捕获类型错误:未定义不是函数。另外,还可以了解如何正确设置继承。这不是任何标记的jQuery问题,所以为什么要使用jQuery?因为这是猫的睡衣。我对$.extend()的使用并不是答案所必需的。这正是我的喜好Organic.prototype.print=function(){}
也可以。使用$作为函数标识符对于jQuery来说并不是唯一的,正如u对于下划线.js来说不是唯一的一样,因此如果使用库,则应该命名正在使用的库。打印函数上的缩进被关闭了1。这不是jQuery的任何标记问题,那你为什么要用jQuery呢?因为这是猫咪的睡衣。我对$.extend()的使用并不是答案所必需的。这正是我的喜好Organic.prototype.print=function(){}
也可以。使用$作为函数标识符对于jQuery来说并不是唯一的,就像uu对于underline.js来说不是唯一的一样,因此如果使用库,则应该命名正在使用的库。打印函数上的缩进关闭1。