JavaScript function Declaration Promotion

Source: Internet
Author: User

First, there are two ways to create functions in JavaScript, function declarations, function expressions, and so on.

1, function declaration.

function Boo () {    console.log (123);} Boo ()

2, function expression.

var function () {    console.log (123)}boo ()

Now say the function declaration promotion. Let's take an example to illustrate it.

Boo (123)function  Boo (x) {    //  123}

Once run, it is known that in a function declaration, a function can be called before the function is created.

Because the function declaration is promoted, the above statement is equivalent to this:

function Boo (x) {    //  123}boo (123)

In the function expression, it will be another result.

Hoo (456)varfunction(y) {    //  uncaught Typeerror:hoo is not a Function}

After running, the discovery will error, why? This is because in a function expression, the function declaration does not advance, it is only the variable that is promoted. So the above statement is equivalent to:

var Hoo;hoo (456function(y) {    //  uncaught Typeerror:hoo is not a function}

Here's another example of a function declaration promotion and a variable declaration promotion.

var function () {    console.log (123)}function  foo () {    console.log (456)}foo ()  

When I first saw this, I did not hesitate to say that the result was 456, but unfortunately, it was wrong. In the following analysis, although the function name of the two functions is the same, but the first method is the function expression, the second is the function declaration, in view of the variable declaration promotion and function declaration promotion, so the above statement is actually equivalent to:

var foo;  //Variable declaration promotion  function foo () {  //function declaration promotion    Console.log (456function() {  //  variable assignment remains in its original position    Console.log (123

So the result is obvious, it's 123.

The last one is easy to confuse.

functionFoo () {getName=function() {Console.log (1); }    return  This;} Foo.getname=function() {Console.log (2)}foo.prototype.getname=function() {Console.log (3)}varGetName =function() {Console.log (4)}functionGetName () {Console.log (5)}foo.getname (); //2
GetName ();//4
Foo (). GetName ();//1 foo () after execution, the global getName that is window.getname to the change to return this, and here this is the window, so the last thing to do is window.getname, so the output 1
GetName ();//1 The above has changed the global getname, so it is still 1
NewFoo.getname ();//2 The new operator executes the constructor function when it instantiates the constructor, that is, the foo.getname executes, and the output 2
NewFoo (). GetName ();//3 First the new Foo () to get an instance, and then execute the GetName method of the instance, at this time, there is no GetName method in the constructor of the instance, it executes the GetName method on the constructor prototype.
New NewFoo (). GetName ()//3

JavaScript function Declaration Promotion

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.