-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (169 loc) · 3.95 KB
/
index.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// const util = require("util");
const handler = {
get:(target,key) => {
// if (key != util.inspect.custom &&
// key != 'inspect' &&
// key != Symbol.toStringTag) {
if(key in target) return target[key];
else
{
let highest = [null,0];
let parents = target.__parents__.filter(link => key in link[0]);
for(let parent of parents)
{
if(parent[1] > highest[1])
{
highest = parent;
}
else if(parent[1] == highest[1]) throw new Error("Ambiguous: Two Parents with same Slot and same Priority");
}
if(highest[0] != null) return highest[0][key];
else throw new Error("Missing: No Such Slot");
}
// }
},
has:(target,key) => {
if(key in target) return true;
else
{
let parents = target.__parents__.filter(link => key in link[0]);
if(parents.length > 0) return true;
else return false;
}
return false;
}
}
function Reuse(obj,clone = false,...parents)
{
const metadata = {};
if(clone)
{
for(let parent of parents)
{
if(parent instanceof Array) parent = parent[0];
if(parent instanceof Function)
{
const temp = new parent();
for(let prop of Object.keys(temp))
{
obj[prop] = temp[prop];
}
}
}
}
for(let i in parents)
{
let parent = parents[i];
if(parent instanceof Array)
{
if(parent[0] instanceof Function) parents[i] = [parent[0].prototype,parent[1]];
else continue;
}
else if(parent instanceof Function) parents[i] = [parent.prototype,1];
else parents[i] = [parent,1];
}
metadata.target = obj;
metadata.__parents__ = parents;
obj.__proto__ = new Proxy(metadata,handler);
return this;
}
function lookup(obj,p)
{
if(obj.__proto__ == p) return true;
if(obj.__proto__ != undefined) return lookup(obj.__proto__,p);
else return false;
}
function iof(obj,p)
{
console.log("Called");
if(p instanceof Function) p = p.prototype;
if (Reflect.has(obj,"__parents__"))
{
console.log(obj)
for(let i of obj.__parents__) {
console.log(i);
return iof(i[0],p);
}
}
else return lookup(obj,p);
return false;
}
Object.prototype.extends = function(...p) { Reuse(this,true,...p); };
Object.prototype.inherit = function(...p) { Reuse(this,false,...p); };
Object.prototype.instanceof = function(p) { iof(this,p); };
function m(...c)
{
return function()
{
this.extends(...c);
}
}
const obj = {
x:10,
y:20,
l:80
}
const obj2 = {
y:30
}
const obj3 = {
z:40,
k:50,
v:60
}
obj3.inherit(obj,[obj2,2]);
class C1
{
constructor()
{
this.a = 100;
this.b = 200;
}
sing()
{
console.log("Stuck in the middle with you!");
}
}
class M1
{
constructor()
{
this.x = 1000;
this.y = 2000;
}
sing()
{
console.log("Sundown you better be safe......");
}
dance()
{
console.log("I'm Bad I'm Bad!");
}
}
M1.prototype.l = 90;
class C2 extends m([C1,3],[M1,2],[obj3,4])
{
constructor()
{
super();
this.i = 10;
this.j = 20;
}
}
let obj1 = new C2();
console.log(obj1.i);
console.log(obj1.j);
console.log(obj1.a);
console.log(obj1.b);
console.log(obj1.x);
console.log(obj1.y);
console.log(obj1.z);
console.log(obj1.k);
console.log(obj1.v);
console.log(obj1.l);
obj1.sing();
obj1.dance();
console.log(obj1.instanceof(C1));
console.log(obj1.instanceof(M1));
console.log(obj1.instanceof(obj3));
console.log(obj1.instanceof(obj2));