Created
September 4, 2012 17:57
-
-
Save ElliotChong/3624204 to your computer and use it in GitHub Desktop.
JavaScript implementation of a Proxy class.
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
/** | |
* JavaScript implementation of a Proxy class. | |
**/ | |
function Proxy(p_target) | |
{ | |
var self = this; | |
self.target = p_target; | |
// Access target's properties | |
self.get = function (p_property) | |
{ | |
return self.target[p_property]; | |
} | |
self.set = function (p_property, p_value) | |
{ | |
self.target[p_property] = p_value; | |
} | |
// Proxy target's functions | |
for (var key in self.target) | |
{ | |
if (typeof self.target[key] === 'function') | |
{ | |
self[key] = function () | |
{ | |
return self.target[key].apply(self.target, arguments); | |
} | |
} | |
} | |
} | |
Proxy.prototype = new Object(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 25 needs a closure for private scope, otherwise all the functions are assigned to the latest proxied function.
Here is how I put it together
Thanks for sharing your code!