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.