Node.js - 使用module.exports作为构造函数 [英] Node.js - use of module.exports as a constructor

查看:694
本文介绍了Node.js - 使用module.exports作为构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Node.js手册:

According to the Node.js manual:


如果您希望模块导出的根是一个函数(例如$) b $ ba构造函数)或者如果要在一个
赋值中导出一个完整的对象而不是一次构建一个属性,则将其分配给
module.exports而不是export。

If you want the root of your module's export to be a function (such as a constructor) or if you want to export a complete object in one assignment instead of building it one property at a time, assign it to module.exports instead of exports.

给出的例子是:

// file: square.js
module.exports = function(width) {
  return {
    area: function() {
      return width * width;
    }
  };
}

并使用如下:

var square = require('./square.js');
var mySquare = square(2);
console.log('The area of my square is ' + mySquare.area());

我的问题:为什么示例不使用square作为对象?以下是否有效,是否使示例更面向对象?

My question: why does the example not use square as an object? Is the following valid and does it make the example more "object oriented"?

var Square = require('./square.js');
var mySquare = new Square(2);
console.log('The area of my square is ' + mySquare.area());


推荐答案

CommonJS模块允许两种方式来定义导出的属性。在任何一种情况下,您都返回一个对象/函数。因为函数是JavaScript中的一等公民,所以它们可以像对象一样行动(技术上它们就是对象)。这就是说你关于使用 new 关键字的问题有一个简单的答案:是的。我将说明......

CommonJS modules allow two ways to define exported properties. In either case you are returning an Object/Function. Because functions are first class citizens in JavaScript they to can act just like Objects (technically they are Objects). That said your question about using the new keywords has a simple answer: Yes. I'll illustrate...

您可以使用 export 提供的变量,用于将属性附加到它。在另一个模块中需要后,这些分配属性变得可用。或者,您可以将对象分配给module.exports属性。在任何一种情况下, require()返回的是对 module.exports 的值的引用。

You can either use the exports variable provided to attach properties to it. Once required in another module those assign properties become available. Or you can assign an object to the module.exports property. In either case what is returned by require() is a reference to the value of module.exports.

模块定义方式的伪代码示例:

A pseudo-code example of how a module is defined:

var theModule = {
  exports: {}
};

(function(module, exports, require) {

  // Your module code goes here

})(theModule, theModule.exports, theRequireFunction);

在上面的例子中 module.exports exports 是同一个对象。很酷的部分是你在CommonJS模块中看不到任何这些,因为整个系统都会为你解决这个问题,你需要知道的是有一个带有exports属性的模块对象和一个指向与module.exports相同的事情。

In the example above module.exports and exports are the same object. The cool part is that you don't see any of that in your CommonJS modules as the whole system takes care of that for you all you need to know is there is a module object with an exports property and an exports variable that points to the same thing the module.exports does.

因为你可以直接将函数附加到 module.exports 你实际上可以返回一个函数,就像它可以作为构造函数管理的任何函数一样(由于函数之间的唯一区别,所以它是斜体的JavaScript中的构造函数是你打算如何使用它。技术上没有区别。)

Since you can attach a function directly to module.exports you can essentially return a function and like any function it could be managed as a constructor (That's in italics since the only difference between a function and a constructor in JavaScript is how you intend to use it. Technically there is no difference.)

所以以下是非常好的代码,我个人鼓励:

So the following is perfectly good code and I personally encourage it:

// My module
function MyObject(bar) {
  this.bar = bar;
}

MyObject.prototype.foo = function foo() {
  console.log(this.bar);
};

module.exports = MyObject;

// In another module:
var MyObjectOrSomeCleverName = require("./my_object.js");
var my_obj_instance = new MyObjectOrSomeCleverName("foobar");
my_obj_instance.foo(); // => "foobar"



需要非构造函数



非构造函数就像函数一样:

Require for non-constructors

Same thing goes for non-constructor like functions:

// My Module
exports.someFunction = function someFunction(msg) {
  console.log(msg);
}

// In another module
var MyModule = require("./my_module.js");
MyModule.someFunction("foobar"); // => "foobar"

这篇关于Node.js - 使用module.exports作为构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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