Several confusing concepts in javascript _ javascript skills

Source: Internet
Author: User
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.

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.