javascript NodeJS 模块导出/原型 - 没有方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14300411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:15:45  来源:igfitidea点击:

NodeJS Module exports / prototype - has no method

javascriptnode.js

提问by Alex

I've got a module that looks like this:

我有一个看起来像这样的模块:

var MyModule = module.exports = function MyModule(opts) {

    opts = (opts === Object(opts)) ? opts : {};

    if (!(this instanceof MyModule)) {
        return new MyModule(opts);
    }

    for (var key in opts) if ({}.hasOwnProperty.call(opts, key)) {
        this.config[key] == opts[key];
    }
};

MyModule.prototype.config = {
    something:'value'
}

MyModule.prototype.put = function put(info, cb) {
   //do stuff

};

However, when I use it like this:

但是,当我像这样使用它时:

var myModule = require('myModule.js');

myModule.put({test}, function(){
    //some callback stuff
});

I get the following error:

我收到以下错误:

TypeError: Object function MyModule(opts) {

opts = (opts === Object(opts)) ? opts : {};

if (!(this instanceof MyModule)) {
    return new MyModule(opts);
}

for (var key in opts) if ({}.hasOwnProperty.call(opts, key)) {
    this.config[key] == opts[key];
} } has no method 'put'

类型错误:对象函数 MyModule(opts) {

opts = (opts === Object(opts)) ? opts : {};

if (!(this instanceof MyModule)) {
    return new MyModule(opts);
}

for (var key in opts) if ({}.hasOwnProperty.call(opts, key)) {
    this.config[key] == opts[key];
} } has no method 'put'

It appears I have something wrong with my MyModule.prototype.put?

看来我的MyModule.prototype.put?

回答by kevin

You wrote :

你写了 :

var myModule = require('myModule.js');

myModule.put({}, function(){
  //some callback stuff
});

Here myModuleis in fact MyModule, a constructor function. So what you are doing is MyModule.put(), a call to a "static" method of MyModule. MyModule.prototype.putdefines an "instance" method so you have to instanciate first :

这里myModule实际上是MyModule一个构造函数。所以你正在做的是MyModule.put(),调用MyModule. MyModule.prototype.put定义了一个“实例”方法,所以你必须先实例化:

var MyModule = require('./myModule.js');

var myModule = new MyModule();
// or as you used `if (!(this instanceof MyModule)) { … }`
var myModule = MyModule();

myModule.put({}, function () {});

So basically your code needs just a pair of ()to work :

所以基本上你的代码只需要一对就()可以工作:

MyModule().put({}, function () {});
// same as
(new MyModule).put({}, function () {});

Récap :

回顾:

var MyModule = function () {
  // Construct object
};

MyModule.staticMethod = function () {
  this; // is bound to `MyModule` function object
};

MyModule.prototype.instanceMethod = function () {
  this; // is bound to the `MyModule` instance object it's called from
};

// Usage

MyModule.staticMethod();

var instance = new MyModule();
instance.instanceMethod();

回答by Sly_cardinal

With this code var myModule = require('myModule.js');your myModulevariable looks like a constructor function, not an instance of myModule.

使用此代码,var myModule = require('myModule.js');您的myModule变量看起来像一个构造函数,而不是 myModule 的实例。

Try instantiating your module first:

首先尝试实例化您的模块:

var MyModule = require('myModule.js');    
var myModule = new MyModule(); // Create an instance of your module.

// Now use it.
myModule.put(/*... your code here ...*/);