【问题标题】:JavaScript TypeError - no method errorJavaScript TypeError - 没有方法错误
【发布时间】:2015-02-14 04:43:57
【问题描述】:

我通过调用此代码得到一个 TypeError:

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:

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 关键字。坦率地说,这似乎有点愚蠢,因为我用“new Player()”调用了 new,但它确实有效。另外,感谢 JS 将术语“方法”与“函数”交织在一起。

【问题讨论】:

  • 你是怎么调用那个函数的?
  • 您能否向我们展示您的代码没有与您的错误消息交错,以及您如何调用该方法?
  • 我更新了问题。这很令人困惑,我真的无法说得更清楚。虽然是合法的问题,所以我很感激反对票和投票结束,谢谢大家。

标签: javascript node.js meteor


【解决方案1】:

它有一个名为extend的函数吗?

不,它没有。如果用作构造函数,则此函数生成的对象将具有:

var obj = new YourFunction();
obj.extend();

new 关键字的使用在这里很重要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多