Node module.exports返回undefined [英] Node module.exports returns undefined

查看:164
本文介绍了Node module.exports返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到Node.js和 module.exports 的问题。我知道 module.exports 是一个返回对象的调用,该对象具有分配的任何属性。

I am having an issue with Node.js and module.exports. I understand that module.exports is a call to return an object, that object having whatever properties it is assigned.

如果我有这样的文件结构:

If I have a file structure like this:

// formatting.js

function Format(text) {
    this.text = text;
}

module.exports = Format;

这个:

// index.js

var formatting = require('./formatting');

有没有办法初始化格式对象并像这样使用它?

Is there a way to initialize a Format object and use it like this?

formatting('foo');
console.log(formatting.text);

每当我尝试这样做时,我收到一条错误,上面写着格式化不是函数。然后我必须这样做:

Whenever I try to do it that way, I get an error that says formatting is not a function. I then have to do it like this:

var x = new formatting('foo');
console.log(x.text);

这看起来很麻烦。

模块中比如 keypress request ,它们可以直接使用,如下所示:

In modules like keypress and request, they can be used right out of the gate, like this:

var keypress = require('keypress');

keypress(std.in);

var request = require('request);

request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage.
  }
})

这是如何工作的?

推荐答案

我建议包装 new 调用它自己的函数然后返回:

I'd suggest wrapping the new call in it's own function then returning that:

function Format(text) {
  this.text = text;
}

function formatting(text) {
  return new Format(text);
}

module.exports = formatting;

这样您仍然可以这样做:

This way you should still be able to do:

var format = formatting('foo');
console.log(format.text);



修改

请求而言,你要记住的一件事是,在JavaScript中,函数仍然是对象。这意味着您仍然可以向它们添加属性和方法。这就是他们在请求中所做的事情,尽管总体来说这有点太复杂,无法解释这个答案中的每个细节。据我所知,他们在请求函数中添加了一堆方法(对象上的函数)。这就是为什么你可以立即调用那些方法,如 request(blah,blah).pipe(blah).on(blah)根据调用<$ c $返回的内容c>请求函数,你可以在它的后面链接一些其他方法。当你使用请求它不是一个对象时,它是一个函数(但在技术上仍然是一个对象)。为了演示函数是如何仍然是对象以及如何向它们添加方法,请查看这个简单的示例代码:

In terms of the request stuff, one thing you have to remember is that in JavaScript, functions are still objects. This means you can still add properties and methods to them. This is what they're doing in request though overall it's a bit too complicated to explain every detail in this answer. From what I can tell, they add a bunch of methods (functions on an object) to the request function. This is why you can immediately call those methods like request(blah, blah).pipe(blah).on(blah) Based on what's returned from calling the request function, you can chain some of the other methods on the back of it. When you're using request it's not an object, it's a function (but still technically an object). To demonstrate how functions are still objects and how it's possible to add methods to them, check out this simple example code:

function hey(){
  return;
}

hey.sayHello = function(name) {
  console.log('Hello ' + name);
} 

hey.sayHello('John'); //=> Hello John

这基本上就是他们正在做的事情,只是更复杂,还有更多事情正在发生。

This is basically what they're doing, just a lot more complicated and with a lot more stuff going on.

这篇关于Node module.exports返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆