Problem: usually we often write var ss = New Person (); SS is an object generated by the ' person class ', but our person method does not write return;
(var ss= person (); returns undefined because no return is written in the METHOD)
Let's start with a regular code.
function person (name) { this. name= name; returnthis; }; function () { returnthis. name; }; var ss= New Person (a); Console.log (ss);
The people of the Earth know that the print results are
And then the second situation
var New function person (name) { this. name = name; return this ; }; function () { returnthis. name; }; var ss= person.apply (obj); Console.log (ss);
Printing results are
You might also think it's normal, so let's see
var New function person (name) { this. name = name; // return this; }; function () { returnthis. name; }; var ss= person.apply (obj); Console.log (ss);
The print result is--undefined
From this we can draw the conclusion that:
1 JavaScript does not have the concept of class, it is important to remember, do not be affected by other class-based language (such as java), that the object must be created by the class, the function is a kind of object
2 New keyword We can think of doing the following several things
var ss = (function var obj = new obj ECT (); // new an object--only to find that new is used here ... How does this explain ...? I still have a study on wood. obj.__proto__ = person.prototype; // The prototype of the object points to the Person's prototype var ret = constructor.apply (obj, arguments); // this--point to obj return typeof ret = = = ' Object '? ret:obj; // }) (person)
Supplementary: the object literal cannot be directly found in its own prototype, it must be delegated to its constructor, and then the constructor will find its prototype
What does Javascript's easy-to-ignore little knowledge Point new do?