NodeJS Module Quick Reference

Please indicate the source of the original reprint: http://agilestyle.iteye.com/blog/2354063

 

Project Directory


 

exports.xxx = function() {}

hello.js

exports.world = function() {
	console.log("Hello World");
};

 

module.exports = xxx

world.js

function Person(name) {
	this.name = name;
}

Person.prototype = {
	constructor: Person,

	sayName: function() {
		console.log(this.name);
	},

	toString: function() {
		return "[Person " + this.name + "]";
	}
};

module.exports = Person;

 

index.js

// exports.world
var hello = require('./hello');
hello.world();

// module.exports
var world = require('./world');
person = new world();
person.sayName();

person = new world('nodejs');
person.sayName();

 

Run


 

Conclusion

The only difference between external references exports.xxx = function() {} and module.exports = xxx module interface is that the latter requires new.

 

Reference

For a more detailed explanation, please refer to

The difference between exports and module.exports

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326980420&siteId=291194637