Examples of variable and function declaration in JavaScript

Source: Internet
Author: User

Examples of variable and function declaration in JavaScript
As shown in 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)

Var name = "xiaoming"; (function () {var name = name | "Xiao Zhang"; console.info (name) ;}) (); // Xiao 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 execution is as follows:



Explanation:
In JavaScript,
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:
Function xxx () {var name2 = undefined; var name = undefined; // other var values will also be pushed to the very beginning. // a bunch of code... //... name2 = name; name = name | "Xiao Zhang"; // a bunch of code}

You can also refer to the Bootstrap Chinese network tutorial How to declare variables and functions in JavaScript (hoist)

Date:

Author: Tie an (http://blog.csdn.net/renfufei)


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.