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)