function promotion and VAR variable hint in JS

Source: Internet
Author: User

In JavaScript, the variables for function declarations and VAR declarations are promoted. But the function declaration is promoted before the variable declared by Var. Even if the function is written in the back.

Look at the following example:

var aa = 221; function AA () {alert (111);} Console.log (AA);  221

This indicates that the function declaration was promoted first. The subsequent var AA declaration overrides the AA function, so the print is------221.

The above statement is in fact the case when the browser resolves.

function AA () {    alert (111);} var  = 221; console.log (AA);

This adds that the function name of the function declaration is no different from the variable name of the normal object (Advanced Programming third edition). Indicates that the function name can be overridden by a variable.

function AA () {       alert (111);} var aa = 221; console.log (aa);   // 221

This is the same as the effect of execution.

Analytical:

function AA () {    alert (111);} var  = 221; console.log (AA);

Also, function is an object that defines properties and methods that can be defined on its body.

The method defined on it is called a class method, or a static method.

belong to this class only. The instance of the class cannot be called.

Example:

function AA () {    alert (111function() {     alert (' I am static method ');} AA.BB ();   // eject "I am static method"

The description function can define a method.

function AA () {alert (111function() {  alert (' I am static method ');} var New AA ();  This sentence pops up 111, which runs the AA function. A.BB ();    // uncaught typeerror:a.bb is not a function (...)

Indicates that an instance of a class in JS cannot call a class method.

function AA () {alert (111function() {  alert (' I am static method ');} AA.C={    cc:5}console.log (AA.C);    // Object {Cc:5}

Defines a property for a function object.

function promotion and VAR variable hint in JS

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.