Here's an example of a generic bind() function, applied directly to an anonymous event handler. The object in XML Request gets bound to its onreadystatechange() event. The search string variable is being pre-applied as well. when the parseMessages() function is called, this pointer and search String are same as they were initially set at event handler:
req.onreadystatechange = function()
{
if (this.readyState == 4)
{
if(this.status == 200) //
{
parseMessages(this.responesText, serchString);
}
else if(this.status == 204) //No Content
{
ClearTable();
}
}
}.bind ( req, searchString );
The bind() function is similar to curry() but includes an extra object parameter to set the pointer (the function will execute within the object's scope).
Function.prototype.bind = function(object)
{
var method = this, oldArguments = arguments.Slice.apply(arguments,[1]);
return function() { return method.apply(object, old Arguments); };
}
0 Comment(s)