Sort out some miscellaneous things that have been learned recently.
1. This is called grouping and judgment execution (I don't know how to make it accurate ).
(a || b || c || d || function(){})()
The first time I saw such code, it was really a headache, but it was good. I had to ask some friends to answer it.
The specific meaning of the Code is: the first () will have a judgment, if a is true, then the code will be executed directly as a (), if a is false, then, judge whether B is true. If B is true, execute B ().
If none of the preceding values are true, the default method is used.
2. Automatic combination of semicolons in js
I don't know when I saw such code.
;(function($){ //do somethings})(jQuery);
For a while, I don't know why I need to add a semicolon in the first line of the Code. Of course, most of the time, there is no problem if I don't add this semicolon. I did not go deep into it myself.
Some friends recently discussed the following code to get a chance to understand the role of this semicolon:
//code 1var a = function(){}a(function(){return 1;})();//code 2var a = function(){}a(function(){return 1 }())
The first code execution will prompt a not defined
The second paragraph is correct.
So why. In fact, the problem is that there is no semicolon behind.
If the semicolon Terminator is not used, js considers the statement below a to be the same by default. The first code will cause an error.
Why is there no error in the second paragraph. This is because of the grouping operator. The function is included in the grouping operator, so the and function are separated.
To avoid errors in the first code segment, we wrote a library segment, but I don't know if the previous Code uses a semicolon.
Therefore, we will add a semicolon at the beginning of our own code. This is just a way to ensure security.
3. Differences between declared functions and function expressions
//code1 foo();function foo(){return 1;}//code2var bar = function foo(){ foo();}foo();
Execute the above two sections and type the code. Soon the result will be displayed. The first code is correct. The second part is foo not defined.
The reason is that the function in the first section is the function declaration.
In js, js will promote the function Declaration, that is, the function declaration will be parsed and evaluated before any expression is parsed and evaluated. Even if your declaration is on the last line of the Code
It will also be parsed and evaluated before the first expression in the same scope.
The second part of the code reports an error, which is also a scope issue. In the scope determined by the function expression, the foo tag is saved to the variable object of the function.
The properties in the variable object cannot be directly accessed using window.
Only activity objects can be accessed directly using Windows. Activity objects are created by global objects.
That is to say, labels can be used to access a function only when the function is a global object.
When the value assignment statement is used, the global object we create is bar instead of foo. Therefore, you can use foo to access objects, but not foo to access objects.