On the promotion _javascript techniques of variable and function declarations in JavaScript

Source: Internet
Author: User
Tags anonymous

Phenomenon:

1. The Declaration of variables and functions in JavaScript is elevated to the top of execution.

2. The elevation of the function is higher than the variable.

3. If an external variable with the same name is declared with Var, the function will no longer look up.

4. Anonymous functions do not ascend.

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

Example:

function declaration elevation above variable declaration

Declare both variable A and function a
var A;
function A () {} 
alert (typeof a);//displays "function", initially proving that the function has precedence over var.

//First declare the function after declaring the variable, prove that the above example is not a function to overwrite the variable
function a () {}
var A; 
Alert (typeof a); The display is still "function", not "undefined", that is, the function has precedence over var.

//Declaration of variables at the same time assignment
function A () {}
var a = 1;
Alert (typeof a); Number, it is not a function at this time.
//Description: "var a=1" is equivalent to "Var a;a=1", that is, the first declaration, after the assignment, "a=1" is equivalent to a new assignment, the nature is number!

The function internally uses Var to define the same variables as the outside, and the function will no longer look up the external variables

var value = ' Hello ';
Function Show () {
  alert (value);
  if (!value) {
    var value = ' function ';
  }
  alert (value);
}
Show ()//Here The call function pops "undefined" in turn, and the "function"

//example is equivalent to var value = ' Hello ';
Function Show () {
var value;//Note This line of
  alert (value);
  if (!value) {
    value = ' function ';//This line removes Var
  }
  alert (value);
}
Show ()//1. If the show inner definition value is not in Var, then an external variable will be used to pop "Hello" and "Hello". 
//2. If you do not define value inside a function, you can also get an external value value.

Anonymous functions do not ascend up

GetName ()
var getName = function () {
  alert (' Closule ')
}
function GetName () {
  alert (' function ')
}
getName ()

//Top of the code is equivalent to
functions GetName () {//The elevation of
  alert (' function ')
}
getName ()
var getName = function () {
  alert (' Closule ')
}
getName ()

//code execution pops up separately Function "," Closule "

Functions in different <script> blocks do not affect each other

<script>
  getName ()
  var getName = function () {
    alert (' Closule ')
  }
<script>
< script>
  function GetName () {
    alert (' function ')
  }
<script>

//Code Execution error: TypeError: GetName is not a function
//Because the GetName () function is undefined in the first <script> block, the anonymous function does not ascend up

The above article on the JavaScript in the variable and function declaration is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.