module.exports = {
methodname: function(arg) {
},
methodname2: function(arg2) {
}
};
exports.somename = {
methodname: function(arg) {
},
methodname2: function(arg2) {
}
};
I suppose this is the same as the function expression pattern mentioned here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
I find it interesting where it says for a function expression:
var object = {
someMethod: function object_someMethod() {}
};
In node.js module.exports is an object:
https://nodejs.org/api/modules.html#modules_module_exports
exports.somename
exports = typeof window === 'undefined' ? global : window;
related links:
https://www.hacksparrow.com/global-variables-in-node-js.html
https://nodejs.org/api/globals.html#globals_global_objects
http://www.mattburkedev.com/export-a-global-to-the-window-object-with-browserify/https://developer.mozilla.org/en-US/docs/Web/API/Window
http://stackoverflow.com/questions/38746211/will-typeof-window-object-always-be-true
http://stackoverflow.com/questions/27133852/export-module-pattern
Examples:
https://github.com/codenorman/solitaire/blob/development/src/gameEngine.jshttps://github.com/rmurphey/js-assessment/blob/master/app/arrays.js
In an experiment when firing up js-assessment (the second example):
window.arraysAnswers gives:
window.arraysAnswers
Object { indexOf: exports.arraysAnswers.indexOf(),
sum: exports.arraysAnswers.sum(), remove: exports.arraysAnswers.remove(),
removeWithoutCopy: exports.arraysAnswers.removeWithoutCopy(), ... }
global gives:
undefined
window gives:
window -> http://127.0.0.1:4444/
I find it interesting where it says for a function expression:
My guess is the exports.name and
" exports = typeof window === 'undefined' ? global : window; "
binds the name to the window object if in the browser, but the
global object when in node. At the moment, I am not sure how this
makes the methods automatically match to the global and
window object.
Maybe that is a nature of being bound to global as the paragraph
"So, in conclusion, there are two ways of creating global variables
in Node.js, one uses the global object, and the other uses
module.exports. What is my recommendation? global method
for small apps, module.exports for big apps." in hacksparrow's
post seems to suggest.
No comments:
Post a Comment