Quote:
Originally Posted by coffeecup
is there another way to write tool[ev.type]? I don't think tool.ev.type would work, would it? 
|
The problem is that what you are trying to do is choose *ONE* of the posslble properties of the
tool object.
Suppose you had:
Code:
var tool = {
mouseup : function( ) { ... },
mousedown : functiion( ) { ... },
keyup : function( ) { ... },
keydown : function( ) { .. }
};
You could *certainly* use either
Code:
tool["mouseup"]
or
tool.mouseup
But in this case, you don't *KNOW* what the value of
ev.type is, do you?
It might be "mouseup" or it might be "keyup" or ...
You just know (or at least you hope!) that
ev.type is *ONE* of the properties of the
tool object.
You *COULD* do
Code:
eval( "tool." + ev.type );
but I'm sure you have heard how evil it is to use
eval( ) and for how many reasons.
So, realistically, the best way *IS* to use simply
Oh, you could write some ugly code like this:
Code:
switch ( ev.type )
{
case "mousedown" : tool.mousedown(ev); break;
case "mouseup" : tool.mousedown(ev); break;
case "keyup" : tool.keyup(ev); break;
case "keydown" : tool.keydown(ev); break;
}
But once you understand it all, isn't it really easier to just do
Code:
tool[ev.type]( ev );
(or the equivalent).