Improvements in JavaScript variables and function declarations

Source: Internet
Author: User
The following small series will bring you an article on the improvements in JavaScript variables and function declarations. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the phenomenon with xiaobian:

1. In JavaScript, the declaration of variables and functions will be promoted to the top for execution.

2. The function is higher than the variable.

3. If the function uses var to declare external variables with the same name, the function will not look up.

4. Anonymous functions will not be upgraded.

5. functions in different script blocks do not affect each other.

Example:

Function declaration is promoted above variable Declaration

// Declare both the variable a and function avar a; function a () {} alert (typeof a); // display "function". It is preliminarily proved that the priority of the function is higher than that of var. // Declare the function first and then declare the variable to prove that the above example does not overwrite the variable function a () {} var a; alert (typeof ); // "function" instead of "undefined" is displayed, that is, the priority of a function is higher than that of var. // Declare the variable and assign the value function a () {} var a = 1; alert (typeof a); // number. This is not a function. // Note: "var a = 1" is equivalent to "var a; a = 1", that is, declaring first, and then assigning values. "a = 1" is equivalent to re-assigning a value, naturally, it is number!

The function uses var to define the same variables as the external variables. The function will no longer look for external variables.

Var value = 'hello'; function show () {alert (value); if (! Value) {var value = 'function';} alert (value);} show () // The "undefined ", "function" // the preceding example is equivalent to var value = 'hello'; function show () {var value; // pay attention to this alert (value); if (! Value) {value = 'function'; // This line removes var} alert (value);} show () // 1. if the show internal defined value in the above column does not use var, external variables will be used. "hello", "hello" will pop up ". // 2. If no value is defined inside the function, the external value can also be obtained.

Anonymous functions are not upgraded.

GetName () var getName = function () {alert ('closule')} function getName () {alert ('function')} getName () // The code above is equivalent to function getName () {// function is upgraded to alert ('function')} getName () var getName = function () {alert ('closule ')} getName () // code execution pops up "function", "closule"

Functions in different script blocks do not affect each other

Script getName () var getName = function () {alert ('closule')} script function getName () {alert ('function ')} script // code execution error: TypeError: getName is not a function // because the getName () function in the first script block is undefined, the anonymous function will not be upgraded.

In this article, the improvements to the variables and function declaration in JavaScript are all the content shared by the editor. I hope to give you a reference and support for PHP.

For more information about how to improve the variables and function declarations in JavaScript, see PHP!

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.