Skip to content

Commit 624cc5a

Browse files
author
traed
committed
Handles circular inheritance
1 parent 91da65d commit 624cc5a

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

multipops.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@ var myObject = {
1010
return obj;
1111
},
1212
'call': function (funcName, params) {
13+
var visited = [];
14+
return this.findFunc(funcName, params, visited);
15+
},
16+
'findFunc': function (funcName, params, visited) {
17+
visited.push(this);
1318
if(this.hasOwnProperty(funcName)) {
1419
return this[funcName](params);
1520
}
1621
var result;
1722
this.prototypes.forEach(
1823
function(obj) {
19-
var call = obj.call(funcName, params)
20-
if(call != undefined && result === undefined) {
21-
result = call;
24+
if(visited.indexOf(obj) == -1) {
25+
var call = obj.findFunc(funcName, params, visited)
26+
if(call != undefined && result === undefined) {
27+
result = call;
28+
return;
29+
}
2230
}
31+
else
32+
console.log("WARNING: Circular inheritance detected!");
2333
}
2434
);
2535
return result;
@@ -37,5 +47,5 @@ try {
3747
console.log(obj3.call('func', 'hello'));
3848
}
3949
catch (err){
40-
console.log("Params must be Array")
50+
console.log(err)
4151
}

0 commit comments

Comments
 (0)