Salient Solutions

wrasslin ones and nones for fun and profit - Sky Sanders' Blog
Get your own ranked flair here
posts - 92, comments - 108, trackbacks - 0

comparing javascript functions

I just learned a hard lesson.

take a look at the following code.

 

salient.delegate.prototype.indexOf = function(fn)
{
    /// <summary>
    /// </summary>nbsp;   {
        for (var i = 0; i < this.eventHandlers.length; i++)
        {
            eventHandler = this.eventHandlers[i]; 
            if (eventHandler.token === fn || eventHandler.fn === fn)
            {
                return i;
            }
        }
    }
    return -1;
}

and compare it to this

 

salient.delegate.prototype.indexOf = function(fn)
{
    /// <summary>
    /// </summary>    if (this.eventHandlers && this.eventHandlers.length)
    {
        for (var i = 0; i < this.eventHandlers.length; i++)
        {
            if (this.eventHandlers[i].token === fn || this.eventHandlers[i].fn === fn)
            {
                return i;
            }
        }
    }
    return -1;
}

 

The first example does not find. The second does. Apparently indexing into an array dereferences the target.This wasn't one of those 3 hour hair pulling sessions, I was aware that I had just refactored that section and not being as fluent in javascript as I could be recognized that there may be a reference issue, because I know the function is in there, i just put it there and I know that previously I was matching. So - inlined the indexer and bingo.  If it aint one thing it is another.... sheesh. 

Print | posted on Saturday, July 18, 2009 3:48 AM |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 1 and type the answer here:

Powered by: