This article mainly introduces several confusing concepts in javascript, which are common problems. We recommend this article to you. If you need it, refer to it.
1.
var name = "The Window";var object = {name : "My Object",getName: function(){return this.name;}};
Here, the getName () method simply returns the value of this. name. The following are several methods to call object. getName ():
Methods and their respective results.
Object. getName (); // "My Object"
(Object. getName) (); // "My Object"
(Object. getName = object. getName) (); // "The Window", in non-strict Mode
In the third case (object. getName = object. getName); equivalent to var fn = (object. getName = object. getName); fn ();
2.
Function outputNumbers (count) {for (var I = 0; I <count; I ++) {// alert (I) ;}var I; // re-declare the variable alert (I); // count} outputNumbers (5 );
JavaScript will never tell you whether to declare the same variable multiple times. In this case, it will only be visible to subsequent calls.
See (however, it will execute variable initialization in subsequent declarations ). Anonymous functions can be used to simulate block-level scopes and avoid this problem.
3.
Function () {// block-level scope} (); // error!
This code may cause a syntax error because JavaScript treats the function keyword as the beginning of a function declaration
The number statement cannot be followed by parentheses. However, the function expression can be followed by parentheses. To convert a function declaration to a function expression,
Just add a pair of parentheses to it as follows.
(Function () {// here is the block-level scope })();
4.
Function outputNumbers (count) {(function () {for (var I = 0; I <count; I ++) {alert (I );}})(); alert (I); // cause an error !}
In the overwritable outputNumbers () function, we insert a private scope outside the for loop. In Anonymous
Any variable defined in the function will be destroyed at the end of execution. Therefore, variable I can only be used in a loop, and will be destroyed after use.
In the private scope, the variable count can be accessed because this anonymous function is a closure that can access
All variables.
This technique is often used outside the function in the global scope to restrict the addition of excessive variables and functions to the global scope.
In general, we should add as few variables and functions to the global scope as possible. In a large scale
In applications, too many global variables and functions can easily lead to name conflicts. By creating a private scope, each developer can
To use your own variables without worrying about messing up the global scope. For example:
(function(){var now = new Date();if (now.getMonth() == 0 && now.getDate() == 1){alert("Happy new year!");}})();
Place the above Code in the global scope to determine which day is July 22, January 1.
The user displays a message to congratulate the new year. The variable now is now a local variable in the anonymous function, and we do not need to be in the global scope.
Create it.
The above is all the content of this article. I hope you will like it.