_javascript tips for early declaring variables or function examples in JavaScript

Source: Internet
Author: User
Tags function examples

As the title shows, look at the example below.

(You can use the Chrome browser and then f12/or right button to review the elements.) Pull up the developer tool and enter the console console input.
(Tips: When the console is entered, Shift+enter can be wrapped in code).

Copy Code code as follows:

var name = "Xiaoming";

(function () {
var name = name | | "Xiao Zhang";
Console.info (name);
}) ()//small Zhang

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

(function () {
var name2= name;
var name = name | | "Xiao Zhang";
Console.info (name, name2);
})(); Xiao Zhang undefined

The screenshot of the execution is as follows:

Explained as follows:

In JavaScript.

Copy Code code as follows:

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

Execution becomes this equivalence form:

Copy Code code as follows:

function xxx () {
var name2 = undefined;
var name = undefined;
Other Var will also be advanced to the starting point
A bunch of code ...
// ...
name2 = name;
Name = Name | | "Xiao Zhang";
A bunch of code
}

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.