javascript - How to properly require a node module, exporting all the functions in it -


i have file stats.js.

it's contents are

(function () {     func1 = function () {      }     func2 = function () {      }      module.exports = this;  }).call(this); 

alegedly, when do

var stats = require("./stats"); 

i should able func1 , func2 stats.func1, stats.func2, right?

well, can't. stats object empty. few traces in stats.js revealed "this" empty object.

what gives?

no shouldnt? format nothing node.js needs in order job.

"what gives" didn't read on how node works. node.js isn't "javascript", it's proramming model richer api , specific behaviours. requires use "module.exports" object, idea read on how use node.

mything.js:

var func3 = function() { ... },     prop = "something"; ... module.exports = {   func1: function() { ... },   func2: function() { ... },   func3: funct3,   prop: prop,   ... }; 

which identical to:

var func3 = function() { ... },     prop = "something",     ...     mylib = {       func1: function() { ... },       func2: function() { ... },       func3: funct3,       prop: prop,       ...     }; ... module.exports = mylib; 

app.js:

var mything = require("mything); 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -