A detailed analysis of constructors in JS

Source: Internet
Author: User

This article is mainly on the structure of JS in a detailed introduction, the need for friends can come to the reference, I hope to help you

In JavaScript, any legitimate function can be used as the constructor of an object, which includes both the system built-in functions and the user-defined functions.   Once a function is executed as a constructor, its internal this property refers to the function itself. Typically, constructors do not return values, they simply initialize the object passed in by the this pointer, and nothing is returned. If a function has a return value, the returned object becomes the value of the new expression.   Formally, whether a function is a constructor or a normal function is the only difference between the execution of the new operator.   The above description actually has a more precise meaning, which means that the function, if there is a return value, is divided into two types of reference type and value type. If the return value of a function is a reference type (an array, object or function, the result of the operation is replaced by its return value when the constructor is executed with the new operator, and the this value in the constructor body is lost and replaced by the returned object.    For example: The code is as follows: function test () {this.a=10;    return function () {return 1; Alert m=new test (); var n=test ();   Alert (m);//Returns the closure alert (n) after return;//Returns the result of the closure run results m and the value of n is the same, are the test function returned closure, and this reference to the object and this.a=10 of the assignment results are discarded. If the return value of a function is a value type, then when the function is constructed with the new operator as a constructor, its return value is discarded.  The result of the new expression is still the object referenced by this.     The code is as follows: function test () {this.a=10; return 1; Alert m=new test (); var n=test (); Alert (m)//returns "Object" alert (n)//returns 1.

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.