Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use Generic bind() Function

    • 0
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 264
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: