The comprehension of a question written in JavaScript

Source: Internet
Author: User

The code is as follows:
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 (); //2GetName ();//4Foo (). GetName ();//1GetName ();//1NewFoo.getname ();//2 = new (Foo.getname) () New no argument listNewFoo (). GetName ();//3 = (new Foo ()). GetName ();//new with argument listNew NewFoo (). GetName ();//3 = new ((New Foo ()). GetName) ()//new with parameter list
Foo.getname ()

Output Result: 2
Principle: He calls the static method of Foo (if the back-end language such as C #, Java, etc.), in fact, because the function in JavaScript itself is the combination of function and object, so functions are both functional and object. Here Foo is the execution of a function on his object.

GetName ()

Output Result: 4
Principle: Maybe you'll wonder why not the 5,function variable is promoted to the top of the scope, and VAR is not defined, so the Var declaration overrides the function declaration. So the result is 4.

Foo (). GetName ()

Output Result: 1
Principle: Foo () is a function call, the GetName is re-assigned in the Foo Execution body (global getName, looked up according to the scope chain), the returned this is window (Foo executes without a call, the default is global), and then. GetName () The global GetName is called (that is, the GetName is written by Foo), so the result is: 1

GetName ()

The output is: 1 principle: Because Foo executes the global getname rewrite, this is called the global GetName. So the result is: 1

New Foo.getname ()

Output Result: 2
Principle:

    • Because new is not a parameter, it is the same as the operation optimization level of new. New has the same level as the. operator and a lower level of new without a parameter. So the. Operator is selected to choose a static method for Foo GetName
    • Then execute new and get an instance of Foo.getname. So the output is: 2
New Foo (). GetName ()

Output Result: 3

Principle:

    • Because new is a parameter, the new Foo () is executed in the order of execution from left to right, as in the. operator sibling.
    • New Foo () is needed to be worthy of our attention because he has a return value. There are two scenarios for the presence of return values:
      • No return is the same effect as returning a base type: The return is an instance object (that is, the current this)
      • Returned as a reference type: Returns an object of this reference type, at which point the instance object is replaced (that is, the current this does not return). Factor new Foo () returns the instance object
    • . GetName (): Select this method for the previous instance, so the output is: 3
New New Foo (). GetName ()

Output Result: 3
Principle: The main operator is the priority of the test, the case should not be used in the development of such a direct use. First new has a parameter, and then the. GetName (. Operator) (why is the. operator, which is because the new non-parametric level is one grade lower), and new has the parameter.

The comprehension of a question written in JavaScript

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.