What is the cross-platform way to add a JavaScript module to the global scope? -



What is the cross-platform way to add a JavaScript module to the global scope? -

i having @ source code of store.js, in particular how adds global scope:

if (typeof module != 'undefined') { module.exports = store } else if (typeof define === 'function' && define.amd) { define(store) } else { this.store = store }

i understand lastly statement this.store = store, how other ones? module , define functions? won't this.store = store work on browsers?

more generally, correct, cross-browser way, add together module global scope?

the first case commonjs, notably used in node.js , flavor of amd (asynchronous module definition). module javascript file gets executed global module object defined. whatever file sets module.exports available other parts of app, , else in file remain private module. here good blog post on it.

the sec 1 flavor of amd, commonly implemented requirejs. it's similar thought commonjs, more commonly found in browser. dojo framework 1 illustration of amd based framework. jquery community getting behind amd lot well. define tells amd scheme giving module rest of app can pull in using require.

the final version mutual scenario of running in plain jane browser. this domwindow, , store object becomes global across whole webpage.

javascript module-pattern global-scope self-invoking-function

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -