JavaScript separation page configuration and code Logic

Source: Internet
Author: User

To make the code clear and structured, you usually need to separate the page configuration parameters and code logic. Define a unique namespace to implement basic so-called structure and behavior separation.

The first method is to provide a global object exposed in the window, which is a TB variable. Then all the code is encapsulated under the Global object.

Copy to ClipboardReference: [www.bkjia.com] // create a namespace
Var TB = {};

/**
* Define the initialization entry function
* @ Method init
* @ Param {JSON} config page configuration parameters
*/
TB. init = function (config ){

Console. log (config. demo );

}

// Page configuration parameter initialization
TB. init ({

'Demo': 'http: // demo.mangguo.org'

});

Alternatively, in the anonymous function mode, the parameter is passed to the anonymous function and is confined to the function body. Isolate the scope chain, the so-called closure.

Copy to ClipboardReference: [www.bkjia.com]/**
* Define anonymous Functions
* @ Param {JSON} config page configuration parameters
*/
(Function (config ){

Console. log (config. demo );

})({
Demo: 'http: // demo.mangguo.org'
});

The first option may be more suitable for organizing complex code, modularity, and granulation.

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.