// Global Variables function func () { // "undefined" var myname = "local"; // "Local" }func ();
Code processing is divided into two phases, the first of which is the variable, function declaration, and the normal format of the parameter creation, which is a phase of parsing and entering the context. The second stage is code execution, function expressions and unqualified identifiers (for declared variables) are created
for (var i = 0; i < myarray.length; i++) { //use myarray[i] to do something}
The trouble with collections is that they query basic documents (HTML pages) in real time. This means that every time you access the length of any collection, you want to query the DOM in real time, and DOM operations are generally more expensive.
for (var i = 0, max = myarray.length; i < Max; i++) { // use myarray[i] do Something}
So, in this loop, you only retrieve the length value once.
for-in
Loops should be used on the traversal of non-array objects, and the use for-in
of loops is also known as "enumerations". hasOwnProperty()
method, you can filter out attributes from the prototype chain when traversing object properties.
//ObjectvarMan ={hands:2, Legs:2, heads:1};//Somewhere in the code.//One method was added to all objectsif(typeofObject.prototype.clone = = = "undefined") {Object.prototype.clone=function () {};}//for-in Cycle for(varIinchMans) { if(Man.hasownproperty (i)) {//FilterConsole.log (i, ":", Man[i]); }}/*Console Display Results hands:2legs:2heads:1*/
To avoid the implicit type conversions that cause confusion, always use the = = = and!== operators when you compare values and expression types.
var zero = 0; if false { // do not execute because zero is 0 instead of false}// Reverse Example if false ) { // executed ...}
Eval () is the devil
// Reverse Example var property = "Name",alert (eval ("obj." + property ); // better. var property = "Name"; alert (Obj[property]);
A function declaration can only appear in a program or function body. In the syntax, they cannot appear in blocks (block) ({...} ), for example, cannot appear in an if, while, or for statement. Because block (block) can contain only statement statements, it cannot contain source elements such as function declarations. On the other hand, a closer look at the rules will also reveal that the only thing that can make an expression appear in block is to make it part of an expression statement. However, the specification explicitly specifies that an expression statement cannot begin with a keyword function. This, in effect, means that the function expression also cannot appear in the statement statement or block (because block is made up of statement statements).
Uncle Tom in-depth understanding of JavaScript learning Notes