Early declaration of variables or functions in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces examples of declaring variables or functions in JavaScript in advance. This article describes a small technique or feature. For more information, see the following example, see the following example.

(You can use the Chrome browser, and then right-click F12/or review the elements. Call out the developer tool and enter the console)
(TIPS: Shift + Enter in the console can be used to wrap code in the middle)

The Code is as follows:


Var name = "xiaoming ";

(Function (){
Var name = name | "Xiao Zhang ";
Console.info (name );
}) (); // Xiaozhang

(Function (){
Name = name | "Xiao Zhang ";
Console.info (name );
}) (); // Xiaoming

(Function (){
Var name2 = name;
Var name = name | "Xiao Zhang ";
Console.info (name, name2 );
}) (); // Small piece of undefined

The execution is as follows:

Explanation:

In JavaScript.

The Code is as follows:


Function xxx (){
// A bunch of code...
//...
Var name2 = name;
Var name = name | "Xiao Zhang ";
// A bunch of code
}

The execution is equivalent to the following:

The Code is as follows:


Function xxx (){
Var name2 = undefined;
Var name = undefined;
// Other var values are also pushed to the starting position.
// A bunch of code...
//...
Name2 = name;
Name = name | "Xiao Zhang ";
// A bunch of code
}

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.