node. JS Getting Started: Similarities and differences between front and back modules

Source: Internet
Author: User

There are usually some modules that can be applied to both the front and back ends, but the JavaScript file is loaded into the script tag in the browser side, unlike node. js. node. JS is packaged to load into the final execution, so that the variables in each file are naturally formed in a closure and do not contaminate global variables. The browser side is usually the bare JavaScript snippet. So in order to solve the problem of front-end consistency, class library developers need to wrap the class library code inside a closure. The following code snippet extracts the definition of underscore from the famous class library.
1(function () { 2     //establish the root object, ' window ' in the browser, or ' global ' on the server.3     varRoot = This; 4     var_ =function(obj) {5         return Newwrapper (obj);6     }; 7     if(typeofExports!== ' undefined ') { 8         if(typeofModule!== ' undefined ' &&module.exports) {9Exports = Module.exports = _; Ten         }  OneExports._ = _;  A     }  -     Else if(typeofdefine = = = ' function ' &&define.amd) { -         //Register as a named module with AMD. theDefine (' underscore ',function () {  -             return _;  -         });  -     }  +     Else {  -Root[' _ ') = _;  +     }  A}). Call ( This);

First, it constructs a closure through the function definition and calls the this as a context object directly to avoid contaminating the internal variables to the global scope. The exports determines whether a local variable is bound to a exports by judging the existence of the variable, and is used as a case for processing in an AMD-compliant environment, depending on the existence of define variables. This refers to a global object (the Window object) only if it is in a browser environment, and the _ variable is assigned to the global object and exported as a method of a global object for external invocation.

So when designing a generic JavaScript class library at the front and back end, there are similar judgments:
if (typeof exports!== "undefined") {     =else  {     this]. Eventproxy = eventproxy;}

That is, if the exports object exists, the local variable is mounted on the exports object, and if it does not exist, it is mounted on the global object.

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.