-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipops.js
51 lines (50 loc) · 1.23 KB
/
multipops.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
var myObject = {
'create': function(params) {
if(params != null && !Array.isArray(params))
throw err
var obj = Object.create(this);
obj.prototypes = [];
if(params != null) {
obj.prototypes = params;
}
return obj;
},
'call': function (funcName, params) {
var visited = [];
return this.findFunc(funcName, params, visited);
},
'findFunc': function (funcName, params, visited) {
visited.push(this);
if(this.hasOwnProperty(funcName)) {
return this[funcName](params);
}
var result;
this.prototypes.forEach(
function(obj) {
if(visited.indexOf(obj) == -1) {
var call = obj.findFunc(funcName, params, visited)
if(call != undefined && result === undefined) {
result = call;
return;
}
}
else
console.log("WARNING: Circular inheritance detected!");
}
);
return result;
},
'prototypes': []
}
try {
var obj0 = myObject.create(null);
obj0.func = function(params){return "func0: " + params};
var obj1 = myObject.create([obj0]);
var obj2 = myObject.create([]);
obj2.func = function(params){return "func2: " + params};
var obj3 = myObject.create([obj1, obj2]);
console.log(obj3.call('func', 'hello'));
}
catch (err){
console.log(err)
}