One, variable
• Global Variables
The two features of JavaScript, unconsciously creating global variables are surprisingly easy. First, you can use variables without even having to declare them; second, JavaScript has an implied global concept, meaning that any variable you don't declare will become a global object attribute (not a global variable in real sense, you can delete it with delete).
Copy Code code as follows:
function sum (x,y) {
Result is not declared, is an implicit global variable
result = x + y;
return result;
}
function foo () {
Use a task chain to make a partial var declaration, B is an implicit global variable
var a = b = 1;
}
Suggestions:
Copy Code code as follows:
function (x,y) {
var a, B;
A = b = 1;//a,b is a local variable
}
var function
Global variables created through VAR (created in programs outside of any function) cannot be deleted. Implicit global variables that are not created by Var (regardless of whether they are created in a function) can be deleted.
Copy Code code as follows:
Define three global variables
var global_var = 1;
Global_novar = 2; Antithesis
(function () {
Global_fromfunc = 3; Antithesis
}());
Attempt to delete
Delete Global_var; False
Delete Global_novar; True
Delete Global_fromfunc; True
Test the deletion
typeof Global_var; "Number"
typeof Global_novar; "Undefined"
typeof Global_fromfunc; "Undefined"
• Declaring variables in single var form
Using a single VAR statement at the top of a function is a more useful form. The initial value of all uninitialized but declared variables is undefined
Copy Code code as follows:
function func () {
var a = 1,
b = 2,
sum = a + B,
MyObject = {},
I
J
function Body ...
}
var spread problem
Copy Code code as follows:
Counter Example
myname = "global"; Global variables
function func () {
alert (myname); "Undefined" var myname = "local"; alert (myname); "Local"} func (), equivalent to:
myname = "global"; Global variable
function func () {
var myname; Equivalent to-> var myname = undefined;
alert (myname); "Undefined"
myname = "local";
alert (myname); "Local"}
Func ();
Two for loop
• Recommended Use
Copy Code code as follows:
function Looper () {
var i = 0,
Max
MyArray = [];
// ...
for (i = 0, max = Myarray.length i < max; i++) {
Use Myarray[i] do something
}
}
Use the following expression instead of i++
Copy Code code as follows:
i = i + 1
i + + 1 The following two kinds of circulation way faster
The first form of change:
var i, myarray = [];
for (i = myarray.length; i–-;) {
Use Myarray[i] do something
}
The second type uses a while loop:
var myarray = [],
i = myarray.length;
while (i–-) {
Use Myarray[i] do something
}
for-in Cycle
Applied to the traversal of a non-array object, the array uses the normal for loop, and the object uses the For-in loop. Using the hasOwnProperty () method, you can filter out properties from the prototype chain while traversing the properties of the object.
Three avoidance of implicit type conversions
• persist in using = = = and! ==
Four avoid using eval, and avoid passing strings to setinterval (), settimeout (), and function () constructors instead.
Five parseint () numerical conversion
It is recommended to assign a value to the cardinality parameter,
Copy Code code as follows:
var month = "06",
Year = "09";
month = parseint (month, 10);//The string starting with 0 is treated as 8-in-process
Year = parseint (year, 10);
Six programming specifications
Constructor Name: Myconstructor ();
General function Name: MyFunction ();
Variable name: firstName;
Private property or method: _secondename,
constants: Pi,max;