Skip to content

Commit 5f91eb9

Browse files
committed
feat(primitive): new
1 parent ec08068 commit 5f91eb9

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

src/primitive/new/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@
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+
78
export 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
}

0 commit comments

Comments
 (0)