A few confusing concepts in JavaScript summarize _javascript skills

Source: Internet
Author: User
Tags anonymous

1.

var name = "The window";
var object = {
name: ' My object ',
getname:function () {return
this.name;
}
};

The GetName () method here simply returns the THIS.name value. Here are several calls to Object.getname ()
Ways as well as the respective results.
Object.getname (); "My Object"
(Object.getname) (); "My Object"
(Object.getname = object.getname) (); "The Window", in the 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 Variable
alert (i);//Count
}

outputnumbers (5); 

JavaScript never tells you whether to declare the same variable more than once; in this case, it will only treat subsequent statements as if not
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 () {//
is a block-level scope
} ();//Error!

This code can cause a syntax error because JavaScript treats the Function keyword as the beginning of a function declaration, and the letter
You cannot follow parentheses after a few declarations. 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 block-level scope
}) ();

4.

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

In this rewritten outputnumbers () function, we insert a private scope outside of 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 a loop and is destroyed after use.
The variable count can be accessed in a 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 of a function in the global scope, limiting 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, participatory effort by many developers
In an application, 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 household displayed a message of congratulation for the new year. Where the variable now is the local variable in the anonymous function, and we don't have to do it in the global scope
Create it in.

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Article

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.