-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomNew.ts
30 lines (26 loc) · 941 Bytes
/
CustomNew.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function SuperHero(name: string) {
this.name = name;
}
function newCreator<T extends (...args: any[]) => any>(
constructor: T,
...args: Parameters<T>
) {
const obj = {};
// first we pass empty object to assign all values to passed object.
const res = constructor.call(obj, ...args);
if (res && typeof res == "object") {
// if it returns object then we return that only.
return res;
} else {
// if nothing is returned then in the object (which now have values attached to them), we set object's prototype as function's prototype.
// obj.__proto__ = constructor.prototype;
Object.setPrototypeOf(obj, constructor.prototype);
return obj;
}
}
const batman = newCreator(SuperHero, "bruce");
const batmanReturns = new SuperHero("bruce 2");
console.log(batman instanceof SuperHero); //true
console.log(batmanReturns instanceof SuperHero); //true
console.log(batman.name)
console.log(batmanReturns.name)