The application part of the js code to set a unified entry

  • 2020-03-30 03:20:58
  • OfStack

Javascript is a scripting language that allows the browser to execute wherever it is downloaded, which makes it easy to program, but it also tends to be messy and fragmented.

In terms of functions, js can be divided into two parts -- the framework part and the application part. The framework part provides the organization of js code, including the definition of global variables, namespace methods, etc. Every page will have the same or similar framework. Application part is the page function logic, different pages will have different functions, different pages application part of the code is not the same.

Give the application part of the js code a unified entry, namely:
 
<script type="text/javascript"> 
function init(){ 
//================================================== 
//  annotation  
//Function, engineer's name, engineer's contact information, time
//================================================= 
(function(){ 
 ... aaaaaaaaaaa 
})(); 
(function(){ 
 ... bbbbbbbb 
})(); 
} 
</script> 

Just call the init() function at the bottom of the page
 
//======init() calls are part of the framework code ========== ======

<script type="text/javascript"> 
init(); 
</script> 

//======init() calls are part of the framework code ========= ====

Note: the code of the framework is mainly divided into:

1. Definition of namespace function

Function init(){} inside the application part of the js

Init () function call (in case there is no init() written in the body, but call, can be written in the following way)
 
<script type=" ... "> 
if(init){ 
init(); 
} 
</script> 

Related articles: