`

NodeJS Module Quick Reference

 
阅读更多

原创转载请注明出处: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

外部引用exports.xxx = function() {}和module.exports = xxx模块接口的唯一区别就是后者需要new一下。

 

Reference

更加详细的解释可以参考

exports 和 module.exports 的区别

 

  • 大小: 1.8 KB
  • 大小: 5.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics