Home  >  Article  >  Web Front-end  >  Summary of several easily confused concepts in javascript_javascript skills

Summary of several easily confused concepts in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:04:181225browse

1.

var name = "The Window";
var object = {
name : "My Object",
getName: function(){
return this.name;
}
};

The getName() method here simply returns the value of this.name. The following are several ways 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; //重新声明变量
alert(i); //计数
}

outputNumbers(5); 

JavaScript never tells you if you declare the same variable multiple times; in that case, it just ignores subsequent declarations
See (however, it does perform variable initialization in subsequent declarations). Anonymous functions can be used to mimic block-level scoping and avoid this problem.

3.

function(){
//这里是块级作用域
}(); //出错!

This code will cause a syntax error because JavaScript treats the function keyword as the beginning of a function declaration, and function
Number declarations cannot be followed by parentheses. However, function expressions can be followed by parentheses. To convert a function declaration into a function expression,
Just add a pair of parentheses to it like below.

(function(){
//这里是块级作用域
})();

4.

function outputNumbers(count){
(function () {
for (var i=0; i < count; i++){
alert(i);
}
})();
alert(i); //导致一个错误!
}

In this rewritten outputNumbers() function, we insert a private scope outside the for loop. Anonymous
Any variables defined in the function will be destroyed at the end of execution. Therefore, the variable i can only be used within the loop and is destroyed after use.
The variable count can be accessed in the private scope because this anonymous function is a closure and it can access
in the containing scope. all variables.

This technique is often used outside functions in the global scope to limit adding too many variables and functions to the global scope.
Generally speaking, we should try to add as few variables and functions to the global scope as possible. In a large
project involving many developers In an application, too many global variables and functions can easily lead to naming conflicts. By creating private scopes, 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!");
}
})();

Put the above code in the global scope, which can be used to determine which day is January 1st; if it reaches this day, it will be used
The user displays a New Year's greeting message. The variable now is now a local variable in the anonymous function, and we don’t have to be in the global scope
Create it in .

The above is the entire content of this article, I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn