File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22 * @Author : Rainy
33 * @Date : 2019-11-14 19:25:01
44 * @LastEditors : Rainy
5- * @LastEditTime : 2019-11 -24 20:52:08
5+ * @LastEditTime : 2022-01 -24 11:37:39
66 */
7+
78export function _new ( ...arg : any ) : any {
9+ // Eg: class Person {} or function Person() {}
10+ // Person.prototype
11+ // Person.prototype.constructor === Person
12+ // person = new Person => person.__proto__ === Person.prototype
13+
14+ // Array.prototype.shift.call(arg) => [...arg].shift()
15+
816 // 1
9- let obj : any = new Object ( ) ;
10- let _constructor = Array . prototype . shift . call ( arg ) ;
17+ // let obj: any = new Object();
1118 // 2
12- obj . __proto__ = _constructor . prototype ;
19+ // obj.__proto__ = _constructor.prototype;
20+ // 1 2
21+ const _constructor = Array . prototype . shift . call ( arg ) ;
22+ const obj = Object . create ( _constructor . prototype ) ;
1323 // 3
14- let result = _constructor . apply ( obj , arg ) ;
24+ const result = _constructor . apply ( obj , arg ) ;
1525 // 4
1626 return typeof result === 'object' && result !== null ? result : obj ;
1727}
You can’t perform that action at this time.
0 commit comments