The difference between exports and module.exports

Source: Internet
Author: User
Tags dio

Original: http://www.hacksparrow.com/node-js-exports-vs-module-exports.html

You must be very familiar with the exports object in the Nodejs module, which you can use to create your module. For example: (assuming this is a rocker.js file)

Exports.name = function () {    console.log (' My name is Lemmy Kilmister ');};

In another file, you refer to this

var rocker = require ('./rocker.js '); Rocker.name (); ' My name is Lemmy Kilmister '

What the hell is module.exports ? Is it legal?

In fact Module.exports , is the real interface,exports is just one of its auxiliary tools. The final return to the call Module.exports is instead of exports.

All the properties and methods collected by the exports are Module.exports assigned. Of course, there is a premise thatModule.exports本身不具备任何属性和方法。如果,Module.exports已经具备一些属性和方法,那么exports收集来的信息将被忽略。

Modify the Rocker.js as follows:

Module.exports = ' ROCK it! '; Exports.name = function () {    console.log (' My name is Lemmy Kilmister ');};

Re-referencing execution rocker.js

var rocker = require ('./rocker.js '); Rocker.name (); Typeerror:object ROCK It! Has no method ' name '

Error found: Object "ROCK it!" No Name method

The rocker module ignores the name method collected by exports and returns a string "ROCK it!". Your module does not necessarily have to return "instanced objects". Your module can be any legitimate JavaScript object--boolean, number, date, JSON, string, function, array, and so on.

Your module can be anything you set to it. If you do not explicitly Module.exports Set any properties and methods, then your module is exports set toModule.exports的属性。

In the following example, your module is a class:

Module.exports = function (name, age) {    this.name = name;    This.age = age;    This.about = function () {        console.log (this.name + ' is ' + this.age + ' years old ');};    };

You can apply it this way:

var = require ('./rocker.js '), var r = new Rocker (' Ozzy ', rocker); R.about (); Ozzy is years old

In the following example, your module is an array:

Module.exports = [' Lemmy Kilmister ', ' Ozzy Osbourne ', ' Ronnie James Dio ', ' Steven Tyler ', ' Mick Jagger '];

You can apply it this way:

var rocker = require ('./rocker.js '); Console.log (' Rockin in Heaven: ' + rocker[2]); Rockin in Heaven:ronnie James Dio

Now you understand that if you want your module to be a specific type to use Module.exports . If the module you want is a typical "instanced object", use exports.

Adding a property to Module.exports is similar to adding a property to exports. For example:

Module.exports.name = function () {    console.log (' My name is Lemmy Kilmister ');};

Again, exports is like this.

Exports.name = function () {    console.log (' My name is Lemmy Kilmister ');};

Note that both of these results do not want to be the same. As mentioned earlier, Module.exports is a true interface, and exports is nothing more than an auxiliary tool. It is recommended to export using exports unless you intend to change from the original "Instanced object" to a type.

The difference between exports and module.exports

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.