Recently began to learn JS, in reading an example of the book, triggered a series of my thinking:
Examples in the book:
function Person (name,age,job) {
var o =new Object ();
O.name=name;
O.age=age;
O.job=job;
O.sayname=function () {
alert (this.name);
};
return o;
}
var friend=new person ("Nicholas", "Software Engineer");
Friend.sayname ();
The person function creates an object, initializes the object with the appropriate properties and methods, and then returns the object, which is exactly the same as the factory pattern, except that the new operator is used and the wrapper function is called the constructor.
Note: The constructor returns the new object instance by default, without returning a value.
See here, I will take the above example of the new keyword removed, and found the same as the original result.
Then, I wrote a few examples to compare:
function Person (name,age) {
This.name=name;
This.age=age;
This.sayname=function () {
alert (this.name);
};
}
var person=new person ("Zhang San", 20);//This is the construction object, the object is constructed, and the new object returned is generated by the parser itself.
var Person=person ("Zhang San", 20);//error person undefined here is a normal function call, there is no return value given, there is a failure.
Person.sayname ();
It is concluded that the use of the new keyword is to call a function as a constructor, that is, to construct the object, the returned object is generated by the parser itself if there is no artificial override for the value returned when the constructor is called. Calling a function without using the New keyword is called for a normal function.
And then I think, what if the function return value is functional?
Example:
function Test () {
this.name = ' Test ';
return function () {return true;}
}
var test = Test ();//test function () {return true;}
var test01 = new Test ();//Construction Object
var test02=test ();//Function call
alert (test01==test02);//false Although the browser runs the same result, the comparison is false, because Javascript's comparison of Object and Function is based on reference.
Browser run monitoring results found:
Finally I change the return value to string type:
function Test () {
this.name = ' Test ';
return "Test";
}
var test = Test ();//test function () {return true;}
var test01 = new Test ();
var test02=test ();
alert (test01==test02);//false
Running monitoring results under browser:
Finally come to a guess:
If the function returns a value type (number, String, Boolean) in the general sense, the new function returns an instance object of the function, and if the function returns a reference type (object, Array, function), Although the new function is equivalent to the result of a direct call to a function, it is two different processes, one constructed and one function called.
This can be confirmed by testing the values in the test function that return different types.
Js uses the new keyword to invoke the difference between a function and a direct call function