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.