new constructor () execution order
1. open an object in the heap memory, denoted as obj
2. Add __proto__ property and obj point constructor .prototype
3. The constructor this point obj
4. perform configuration the function statement
if the constructor does not return this value or substantially reutrn or type (number, string, boolean, nullis, obj memory heap address is returned; if the reference type return, the return value of this reference type
parodies new behavior
function People (name, Age, Phone) { the this .name = name; the this .age = Age; the this .phone = Phone; // if there is no `constructor or substantially reutrn or return this type of` (number, string, boolean , the value of null, undefined), then returns obj memory address in the stack; if the reference type `` return, the return value of this reference type // return null; // no effect // return {}; // returns this object // return function () {}; // this function returns } function _new(...args) { constructor the let = args [0]; // Get constructor the let the Object.create obj = (constructor.prototype); // Create an empty object, and the prototype prototype point constructor let res = constructor.call (obj, .. .args.slice (. 1)); // Call this point to force the first parameter IF (( typeof RES === 'Object' || typeof ! RES === 'function') && RES = null ) { return res; } else { return obj; } } let a = _new(People, 'aa', 20, 132456); let na = new People('aa', 20, 132456); console.log(a, na); // Run Results People {name: 'AA', Age: 20 is, Phone: 132456 } People { name: 'aa', age: 20, phone: 132456 }
----------------
Disclaimer: This article is CSDN blogger "diffuse sparse mad" in the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/lyt_angularjs/article/details/86623988