First, execute the function immediately
for (var i = 0; i < elems.length; i++) { (function (lockedinindex) { elems[i].addeventlistener (' click ', Functio N (e) { e.preventdefault (); Alert (' I am Link # ' + lockedinindex); }, ' false '); }) (i); Enter the argument}
Use the immediate execution function to achieve the purpose of not exposing private members
var Module1 = (function () {var _count = 0; var m1 = function () {//...}; var m2 = function () {//...}; return {m1:m1, m2:m2}; })(); Unable to read _count
Compare immediate execution functions
<! DOCTYPE html>
Second, the module specification at present the main module specification has COMMONJS and AMD 1.commonJScommonJS is the server-side module specification, Nodejs follows this specification. Because the server JS file is stored locally, loading speed is fast, so its loading module (a file is a module) mode for synchronous loading, not applicable to the browser.function Foobar () { This.foo = function () { console.log (' Hello foo '); } This.bar = function () { console.log (' Hello bar ');} } exports.foobar = Foobar;//export for external communication
var foobar = require ('./foobar '). Foobar, test
2.AMD takes the asynchronous load module, after loading completes through the callback function to implement the corresponding operation, applies with the browser.Require ([' math '], function (math) {Math.add (2, 3); });
Original address: http://www.ruanyifeng.com/blog/2012/10/javascript_module.html HTTP://WWW.CNBLOGS.COM/TOMXU/ARCHIVE/2011/12 /31/2289423.htmlhttp://javascript.ruanyifeng.com/nodejs/commonjs.html"Turn" require.js study notes (i)