First, develop good coding habits, improve maintainability
1. Avoid defining global variables and functions, and solving global variables. The simplest method of "pollution" is to encapsulate variables and methods on a variable object, making it a property of the object:
1 var Mycurrentaction = {2 length:0 ,3 function() {}, 4 function () {}5 }
You can also include global variables in a local scope, and then complete the definition of these variables and the logic used by the variables in this scope:
1 (function() {2 var length = 0; 3 function init () {} 4 function action () {} 5 }) ();
The module interacts with the return statement (that is, encapsulating the code logic and exposing the interface that needs to be accessed):
1 var mycurrentaction = (function() {2 var length = 0; 3 function init () {} 4 function action () {} 5 return {6 init:init7 }8 }) ();
"Web front end development Best Practices Series" JavaScript Chapter