tarini
23 Jul 2009, 12:20 PM
Hi guys...
I created a new version of Function.prototype.createInterceptor that pass to the original function the returned form of intercepting function instead of original arguments.
As in the original createInterceptor, if the passed function will return false, original function won't be called
Usage example:
var myFunction = function(name) {
alert("My name is " + name);
}
var myInterceptingFunction = myFunction.createBindInterceptor(new Function(name) {
return [name.toUpperCase()];
});
myFunction("alberto"); //will alert "alberto"
myInterceptingFunction("alberto") //will alert "ALBERTO"
Here's the code:
Function.prototype.createBindInterceptor = function(fcn, scope){
if(typeof fcn != "function"){
return this;
}
var method = this;
return function() {
fcn.target = this;
fcn.method = method;
var result = fcn.apply(scope || this || window, arguments);
if(result === false){
return;
}
return method.apply(this || window, result || arguments);
};
}
Hope it will be useful to someone ;)
I created a new version of Function.prototype.createInterceptor that pass to the original function the returned form of intercepting function instead of original arguments.
As in the original createInterceptor, if the passed function will return false, original function won't be called
Usage example:
var myFunction = function(name) {
alert("My name is " + name);
}
var myInterceptingFunction = myFunction.createBindInterceptor(new Function(name) {
return [name.toUpperCase()];
});
myFunction("alberto"); //will alert "alberto"
myInterceptingFunction("alberto") //will alert "ALBERTO"
Here's the code:
Function.prototype.createBindInterceptor = function(fcn, scope){
if(typeof fcn != "function"){
return this;
}
var method = this;
return function() {
fcn.target = this;
fcn.method = method;
var result = fcn.apply(scope || this || window, arguments);
if(result === false){
return;
}
return method.apply(this || window, result || arguments);
};
}
Hope it will be useful to someone ;)