JavaScript TypeError-无方法错误
通过调用以下代码,我得到一个TypeError:JavaScript TypeError-无方法错误,javascript,node.js,meteor,Javascript,Node.js,Meteor,通过调用以下代码,我得到一个TypeError: player = new Player({name:''}); Player = MeteorModel.extend({ schema:{ name:{type:String,}, value:{} }, belongsTo:{ meteorCollection:'', //which collection to add to methodName: '' //which method t
player = new Player({name:''});
Player = MeteorModel.extend({
schema:{
name:{type:String,},
value:{}
},
belongsTo:{
meteorCollection:'', //which collection to add to
methodName: '' //which method to save on server side
},
print: function(){
console.log(this);
},
create: function(prototype){
this.fields.name = prototype.name;
this.type = prototype.type;
return this;
},
validate: function(){
return true;
}
});
var MeteorModel = function(){
this.extend = function(prototype){
this.model = prototype;
return this;
}
};
我应该如何处理此JavaScript类型错误:
TypeError: Object function(){
this.extend = function(prototype){
this.model = prototype;
return this;
}
}
has no method 'extend'
它有一个名为extend的函数?怎么了
我解决问题的方法是:
我把代码改成了这个
var MeteorModelFunction = function(){
this.extend = function(prototype){
this.model = prototype;
return this;
}
};
MeteorModel = new MeteorModelFunction();
因此,我使用了新的关键字,正如建议的那样。坦率地说,这似乎有点愚蠢,因为我用“new Player()”调用了new,但它是有效的。另外,感谢JS将术语“方法”与“函数”交织在一起
它有一个名为extend的函数
不,没有。如果用作构造函数,则此函数生成的对象将具有:
var obj = new YourFunction();
obj.extend();
关键字的使用在这里很重要。您如何调用该函数?能否向我们展示您的代码没有与错误消息交错,以及您如何调用该方法?我更新了问题。这让人困惑,我真的说不清楚了。这是一个合法的问题,所以我很感谢反对票和结束票,谢谢大家。