Skip to content

Commit ec08068

Browse files
committed
feat(object): create
1 parent 497e249 commit ec08068

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/Object/create/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @Author: Rainy [https://github.com/rain120]
3+
* @Date: 2022-01-24 11:43:11
4+
* @LastEditors: Rainy
5+
* @LastEditTime: 2022-01-24 11:47:54
6+
*/
7+
8+
export function create(prop: any, options?: Object) {
9+
if (!['object', 'function'].includes(typeof prop)) {
10+
throw new TypeError(`Invalid property with ${prop}`);
11+
}
12+
13+
const Ctor = function() {};
14+
15+
Ctor.prototype = prop;
16+
17+
// @ts-ignore
18+
const obj = new Ctor();
19+
20+
if (options) {
21+
// @ts-ignore
22+
Object.defineProperties(obj, options);
23+
}
24+
25+
if (prop === null) {
26+
obj.__proto__ = null;
27+
}
28+
29+
return obj;
30+
}

0 commit comments

Comments
 (0)