"Turn" require.js study notes (i)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.