proxy
Creates callback functions that have this set correctly.
can.Construct.proxy(callback, [...args])
Creates a static callback function that has this set to the constructor
function.
Parameters
-
callback
{function() | String | Array<function() | String>}the function or functions to proxy
-
args
{[}parameters to curry into the proxied functions
Returns
{function()}
a function that calls callback with the same context as the current context
construct.proxy(callback, [...args])
Creates a static callback function that has this set to an instance of the constructor
function.
Parameters
-
callback
{function() | String | Array<function() | String>}the function or functions to proxy.
-
args
{[}parameters to curry into the proxied functions
Returns
{function()}
a function that calls callback with the same context as the current context
can.Construct.prototype.proxytakes a function and returns a new function that, when invoked, calls the given function with the samethisasproxywas called with.Here is a counter that increments its count after a second:
(Recall that setTimeout executes its callback in the global scope.)
If you pass the name of a function on the
thisthatproxyis called with,proxywill use that function. Here's how you write the previous example using this technique:Currying arguments
If you pass more than one parameter to
proxy, the additional parameters will be passed as parameters to the callback before any parameters passed to the proxied function.Here's a delayed counter that increments by a given amount:
Piping callbacks
If you pass an array of functions and strings as the first parameter to
proxy,proxywill call the callbacks in sequence, passing the return value of each as a parameter to the next. This is useful to avoid having to curry callbacks.Here's a delayed counter that takes a callback to call after incrementing by a given amount:
proxyon constructorscan.Construct.proxy also adds
proxyto the constructor, so you can use it in static functions with the constructor asthis.Here's a counter construct that keeps its count staticly and increments after one second:
See also
can.proxy is a way to proxy callbacks outside of
can.Constructs.