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)



Urgent. Question about javascript variable declaration?

First:

<Script language = "javascript">
Var kk = "123 ";
Ss = "789 ";
Document. write (kk + '<br>' + ss );
</Script>

The declared variable is written in this way var kk = "123", ss = "789 ";
Now I write the ss to the next line. Isn't the ss a variable declared by var at this time?

**************************************** *************
A:
Var kk = "123"; // check whether the error is "; (semicolon)". The correct one is comma (,);
Correct definition method:
Var kk = "123", // note that comma is used here
Ss = "789 ",
Aa = "11 ",
Bb = "22 ",
Cc = "33 ",
// Infinite variables can be defined.
Xx = "77 ",
Yy = "88 ",
Zz = "99"; // a semicolon is used only when the last one arrives, and a semicolon must be used;

Second:

<Script language = "javascript">
Var kk = "123 ";
Kk = "789 ";
Document. write (kk + '<br>' + ss );
</Script>

Originally, I wrote var kk = "123", kk = "789 ";
Now I have changed kk = "789" to the next line. Is kk declared again with var in the same variable?
**************************************** *************
A:
First, the kk value defined here is the same as what you think: 789, And the syntax error in "first" is also displayed here.

Third:
<Script language = "javascript">
Var
Kk = "123 ";
Ss = "789 ";
Document. write (kk + '<br>' + ss );
</Script>
If I write a single line of var, can kk and ss be declared?
**************************************** *************
A:
Yes, it's exactly the same as what you think. Here we declare both kk and ss variables;
()... Remaining full text>

Function declaration in javascript

First standard function declaration
The second one is similar to the first one, but it is used as an anonymous function value-assigned variable. In terms of semantics, however, the final effect is the same. A func2 variable name will be generated in the current context.
Third, I rarely use this method, but it should also work. It is actually equivalent to var func3, func4; func3 = func4 = function (){}
The fourth is to use the Function constructor. This constructor uses a string to construct a Function dynamically. For example, you can concatenate a Function using a string, for example, new Function ("alert var" + varName + "+ var" + varName2) is used to pre-compile javascript with high performance. This method is often used by the javascript template engine.

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.