我是node.js(和stackoverflow)的新手,但尚未找到确切的解释。
这可能是一个试验性的答案,但希望它将对也从Python /其他面向对象框架过渡的其他人有所帮助。
我看过其他有关js原型概念的文章,然后还有其他文章解释了node.js的module.exports。
我正在研究Ghost CMS,它们同时使用。我似乎无法找出为什么他们在某些情况下会选择一个而不是另一个。
感谢您提供任何帮助,即使它将我指向其他链接也是如此。
使用node.js,module.exports是公开模块的公共接口的方式。
/* my-module.js */
exports.coolFunction = function(callback) {
// stuff & things
callback(whatever);
};
导入/要求后,另一个模块可以使用此接口:
/* another-module.js */
var myModule = require('my-module');
myModule.coolFunction(function(error) { ... });
另一方面,原型(一种普通的Javascript功能)对于定义从构造函数实例化的对象的共享属性和方法很有用。
function User() {
this.name = null;
}
User.prototype.printGreeting = function() {
console.log('Hello. My name is: ' + this.name);
};
var user = new User();
user.name = 'Jill';
user.printGreeting();
干杯。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句