forked from ibl/Prototypical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subClassOf.js
50 lines (47 loc) · 1.94 KB
/
subClassOf.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
console.log('subClassOf.js added .subClassOf to Object')
Object.prototype.subClassOf=function(x,fun){
if(typeof(x)=='object'){ // defalt inheritance using the prototype as the object in the local scope
var k = Object.getOwnPropertyNames(x)
for(i=0;i<k.length;i++){
if(!this[k[i]]){this[k[i]]=x[k[i]]}
}
if(fun){fun(this)}
//else{console.log(this,'subClassOf',x)} // this could become annoying for someone looking at the console
}else{ // assume x is the URL of prototype object and go get it
var that = this;cbFun = fun
var load = function(url){ // XMLHttpRequest
var r = new XMLHttpRequest();
r.onload=function(){
if(!cbFun){cbFun=function(){}}
// notice check for .noEval flag in the callback function
if(cbFun.noEval){
// if true then pack domain and range as attributes of the callback
// i.e. the relationship between domain and range if completely up to the callback
var x = {} // subClassOf will not migrate anything from range
fun.domain=that
fun.range=this
cbFun=function(x){
//var subClassOf_domain=fun.domain, subClassOf_range=fun.range
return fun(fun)
}
} else {
var x = eval("("+this.responseText+")")
}
that.subClassOf(x,cbFun)
};
r.open("GET",url,true);
r.send();
return this
}
load(x)
}
return this
}
// examples:
//
// a={b:9}
// a.subClassOf({c:10})
//
// // slide 34 of http://www.slideshare.net/stefandecker1/stefan-decker-keynote-at-cshals
// HongGeesCar={color:"blue"}
// HongGeesCar.subClassOf("https://www.googledrive.com/host/0BwwZEXS3GesiTjlHSmlOcEJaeDA/Prototypical/StefansCar.json")