A few confusing concepts in JS

Source: Internet
Author: User

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 calls to 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

The third case (Object.getname=object.getname) is 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-declaring variables
alert (i); Count
}

Outputnumbers (5);

JavaScript never tells you whether the same variable is declared more than once; in this case, it will only treat subsequent declarations as
See (However, it performs variable initialization in subsequent declarations). Anonymous functions can be used to mimic block-level scopes and avoid this problem.

3.

function () {
This is a block level scope.
}(); Error!
This code causes a syntax error because JavaScript treats the Function keyword as the beginning of a declaration of functions, and the letter
The number declaration cannot be followed by parentheses. However, the function expression can be followed by parentheses. To convert a function declaration into a function expression,
Just add a pair of parentheses to it like so.
(function () {
This is a block level scope.
})();

4.

function Outputnumbers (count) {
(function () {
for (var i=0; i < count; i++) {
alert (i);
}
})();
alert (i); Cause an Error!
}

In this rewritten outputnumbers () function, we insert a private scope outside the For loop. In Anonymous
Any variables defined in the function will be destroyed at the end of execution. Therefore, the variable I can only be used in loops and is destroyed after use.
The variable count can be accessed in the private scope because the anonymous function is a closure that can access the containing scope of the
All variables.

This technique is often used outside the function in the global scope to limit the addition of too many variables and functions to the global scope.
In general, we should always add variables and functions to the global scope as little as possible. In a large number of developers who are involved
In applications, too many global variables and functions can easily lead to naming 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!");
}
})();
Put the above code in the global scope, which can be used to determine which day is January 1;
The user displays a message to congratulate the New Year. Where the variable now is a local variable in the anonymous function, and we don't have to be in the global scope
To create it in.

A few confusing concepts in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.