Chapter 2 JavaScript ModeBasic SkillsI talked about some of the specifications and suggestions in the previous programming section, and some pitfalls that are often ignored and easy to step on.
The following is only part of the contentSummaryAnd summary for memo. If any errors or omissions exist, please note. For more information, see the original book.
1. Trap of chain assignment
Function func () {var innervar = globalvar = 20;} func (); console. Log (typeof globalvar); // The output result is?
What is the final output result above? I believe many people will not hesitate to say undefined, OK?
The truth is: Number
Cause:The priority of the right-to-left operator. First, expression B = 0 is given a higher priority. B is not declared at this time. The Return Value of the expression is 0, which is assigned to the local variable a declared by VAR, as shown in the following code.
var a = (b = 0);
Suggestion: declare all the variables with chained values and assign values again.
Function Foo () {var a, B; a = B = 20; // all local variables}2. Side effects of variable release
Implicit global variables are slightly different from clearly defined global variables. The difference is whether the delete operator can be used to cancel variables.
- Global variables created using VAR (these variables are created outside the function) cannot be deleted.
- You can delete an implicit global variable created without using VAR (although it is created inside the function ).
This indicates that the implicit global variable is not a real variable, but a global object attribute. The attribute can be deleted using the delete operator, but the variable cannot.
// Define three global variables var global_var = 1; global_novar = 2; // reverse mode (function () {global_fromfunc = 3; // reverse mode })(); // attempt to delete global_var; // falsedelete global_novar; // truedelete global_fromfunc; // true // test the deletion typeof global_var; // 'number' typeof global_novar; // 'undefined' typeof global_fromfunc; // 'undefined'
In es5 strict mode, assigning a value to a variable that is not declared throws an error.
3 For-in traps
var person = { name: 'casper', age: 11};for(var key in person){ console.log(key);}
Run the above Code and the output result is as follows:
Output: Name output: Age
How can I slightly modify the above Code?
var person = { name: 'casper', age: 11};Object.prototype.getName = function(){}; for(var key in person){ console.log(key);}
The output result is:
Output: Name output: Age output: getname
Suggestion: do not add a built-in object prototype. Unless necessary, communicate well within the Team to ensure that other team members do not encounter any strange errors.
4. Pay attention to the differences between eval and new functions.
- Eval () will affect the scope
- The code in new function () will run in the local function space. Therefore, any variable defined by VAR in the code will not automatically become a global variable.
- No matter where the function is executed, it can only see the global scope
Let's look at the sample code:
console.log(typeof un); //'undefined'console.log(typeof deux); //'undefined'console.log(typeof trois); //'undefined'var jsstring = "var un = 1; console.log(un);";eval(jsstring); //logs "1"jsstring = "var deux = 2; console.log(deux);";new Function(jstring)(); //logs "2"jsstring = "var trois = 3; console.log(trois);";(function(){ eval(jsstring);})(); //logs "3"console.log(un); //'number'console.log(typeof deux); //'number'console.log(typeof trois); //'undefined'
From the code example above, we can clearly see the first two points. For the third point, see the following code example:
(function(){ var local = 1; eval("local = 2; console.log(local);"); //logs 3 console.log(local); //logs 3})();(function(){ var local = 1; new Function("console.log(typeof local);")(); //logs 'undefined'})();