Compatible with CommonJS and Commonjs-like specifications (1~38)

Source: Internet
Author: User

COMMONJS is a specification for server-side modules, and node. JS uses this specification.

According to the COMMONJS specification, a single file is a module. Each module is a separate scope, meaning that variables defined inside the module cannot be read by other modules unless they are defined as properties of the global object.

If jquery is to be used in node, there are two issues to solve:

1. Import by Module.exports (Commonjs module Introduction)

2. Because it is a server environment, there is no window variable.

/*! * JQuery JavaScript Library v2.1.1 * http://jquery.com/* * Includes sizzle.js * http://sizzlejs.com/* * Copyright  2005, JQuery Foundation, Inc. and other contributors * released under the MIT license * http://jquery.org/license * date:2014-05-01t17:11z*/(function(Global, factory) {if(typeofmodule = = = "Object" &&typeofModule.exports = = = "Object" ) {        //for CommonJS and commonjs-like environments where a proper window is present,        //execute the factory and get JQuery        //For environments that does not inherently posses a window with a document        //(such as node. js), expose a jquery-making factory as Module.exports        //This accentuates the need for the creation of a real window        //e.g. var jQuery = require ("jquery") (window);        //See ticket #14549 for more infoModule.exports = global.document?Factory (Global,true ) :            function(W) {if( !w.document) {Throw NewError ("jQuery requires a window with a document" ); }                returnFactory (W);    }; } Else{Factory (global); }//Pass This if window was not defined yet}(typeofWindow!== "undefined"? window: This,function(window, noglobal) {//Factory code ....}))

According to the code, jquery uses an immediate function to determine if it is the COMMONJS specification (typeof module = = = "Object" && typeof module.exports = = = "Object"), is not called the library function, is then judged that there is no document (go to this step is to Judge no window, open no window mode), there is the call library function,

None returns an anonymous function that waits for the incoming window.

Compatible with CommonJS and Commonjs-like specifications (1~38)

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.