Created
June 22, 2011 21:30
-
-
Save FireyFly/1041265 to your computer and use it in GitHub Desktop.
Playing around with ES Harmony Proxies
This file contains 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
Proxy ?= require "node-proxy" | |
desc = | |
getPropertyDescriptor: (name) -> | |
value: (Proxy.createFunction desc, | |
(rest...) -> | |
(arg1, args...) -> | |
restIdx = 0 | |
if typeof arg1[name] == 'function' | |
arg1[name].apply arg1, rest.map (arg) -> | |
if arg == _ | |
args[restIdx++] | |
else | |
arg | |
else | |
arg1[name]) | |
_ = Proxy.create desc | |
# Testing it... | |
Number.prototype.add = (y) -> this + y | |
console.log (_.add 10)(20) | |
console.log (_.add _)(10, 20) | |
console.log [0, 10, 20, 30].map _.toString 16 | |
console.log "hello and world".split(" ").map _.length() # Currently a dummy function call is needed here. | |
console.log _.length() "hello" | |
# | |
# $ coffee test2.coffee | |
# 30 | |
# 30 | |
# [ '0', 'a', '14', '1e' ] | |
# [ 5, 3, 5 ] | |
# 5 | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment