Created
March 30, 2012 15:55
-
-
Save vstarck/2252415 to your computer and use it in GitHub Desktop.
experimentos varios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
var list1 = list(1, 2, 3); | |
var list2 = list(4, 5, 6); | |
var list3 = list1('+')(list2); | |
list3('add')(7); | |
list3('debug')(); // "list (7) [1,2,3,4,5,6,7]" | |
*/ | |
var x = function(proto) { | |
var subject, proxy; | |
if(!proto) { | |
proto = {}; | |
} | |
for(var p in x.__proto) if(x.__proto.hasOwnProperty(p)) { | |
if(!proto.hasOwnProperty(p)) { | |
proto[p] = x.__proto[p]; | |
} | |
} | |
subject = Object.create(proto); | |
proxy = function() { | |
return subject.__get.apply(subject, arguments); | |
} | |
subject.proxy = proxy; | |
proxy.subject = subject; | |
return proxy; | |
}; | |
x.__proto = { | |
__get: function(prop) { | |
if(!this[prop]) { | |
return this.__noSuchProperty(prop); | |
} | |
if(typeof this[prop] == 'function') { | |
return this[prop].bind(this); | |
} | |
return this[prop]; | |
}, | |
__noSuchProperty: function(prop) { | |
throw prop + ' not found'; | |
} | |
}; | |
var list = function() { | |
var instance; | |
instance = x(list.__proto); | |
instance.subject.__array = [].slice.call(arguments); | |
return instance; | |
}; | |
list.__proto = { | |
'add': function(element) { | |
this.proxy('toArray')().push(element); | |
return this.proxy; | |
}, | |
'copy': function() { | |
return list.apply(null, this.proxy('toArray')().concat()); | |
}, | |
'sort': function(fn) { | |
return this.proxy('copy')('sort')(fn); | |
}, | |
'sort!': function(fn) { | |
this.proxy('toArray')().sort(fn); | |
}, | |
'size': function() { | |
return this.proxy('toArray')().length; | |
}, | |
'toArray': function() { | |
return this.__array; | |
}, | |
'+': function(another) { | |
return list.apply( | |
null, | |
this.proxy('toArray')().concat(another('toArray')()) | |
); | |
}, | |
'debug': function() { | |
return 'list (' + this.proxy('size')() + ') [' + this.proxy('toArray')() + ']'; | |
} | |
} | |
var list1 = list(1, 2, 3); | |
var list2 = list(4, 5, 6); | |
var list3 = list1('+')(list2); | |
list3('add')(7); | |
list3('debug')(); // "list (7) [1,2,3,4,5,6,7]" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment