new processes have done what

new process

- 新生成一个对象
- 链接到原型(继承该函数的原型)
- 绑定this(将原型中的属性和方法加入到this引用的对象中)
- 返回新对象

Own realization of a new:

1
2
3
4
5
6
7
8
9
10
11
12
Create function () { 
// create a null object
the let obj = new new Object ()
// constructor obtain
the let Con = [] .shift.call (arguments)
// set point new object constructor __proto__ property prototype property
obj .__ = Con.prototype proto__
// this binding, this refers to the function a new instance of an object.
the Result = Con.apply the let (obj, arguments The)
// make sure it is a new target
return typeof the Result === 'Object' the Result:? obj
}

// concise version of the new implementation process

1
2
3
4
5
6
7
8
9
10
newfunc function (constructor) { 
// Step 1: Create an empty object obj
var obj = {};
// Step: prototype object constructor prototype constructor assigns obj
obj .__ proto__ = constructor.prototype;
/ / the third step: in this constructor constructor point obj, and to perform internal operations constructor immediately
constructor.apply (obj);
// step Four: returns this object
return obj;
}

Original: Big Box  new process have done what


Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11652412.html