-1

Can anybody tell me what this code means?

var func = tool[ev.type];
if (func) {
  func(ev);
}

tool is a function declared upun this sentece.

1
  • Functions are objects in JS. This basically says: Assign func to whatever's in the tool function (object) with object key ev.type. If it's a truthy value, execute it. Commented Apr 12, 2013 at 0:31

2 Answers 2

1

It's referencing a property on the function (Object) "tool", which apparently contains another function in itself. Testing to make sure the function exists, then invoking that function.

tool is an object filled with event handlers, the property names in the object "tool" correspond to the different type of events. The code is referencing the property in the "tool" object based on the event "type." For example:

var tool = {
    'click': function(evt) {}, // event handler for click
    'mousedown': function(evt) {}, // event handler for mousedown
    'mouseup': function(evt) {}, // event handler for mouseup
}

// User presses the mouse button down and doesn't release.
//       ev.type == 'mousedown'
//

// Save the property value (which should be an event handler)
// to a variable.
var func = tool[ev.type];

// Make sure func is defined before attempting to invoke it.
if (func) {
    // func is defined, invoke it and pass the event object to it
    func(ev);
}

Hope this helps! :)

Sign up to request clarification or add additional context in comments.

2 Comments

but, where ev comes from? Take a look at the hole code please. devfiles.myopera.com/articles/649/example2.js
ev is also an object, but you haven't posted it's declaration so there's no way to tell where it's coming from.. I would assume, by way of convention, that ev is a short variable name for the window.event object. Which does indeed have a "type" property.
1

In addition to the answer above, looking at a simpler (and full) example may be helpful in understanding what's going on here, from a pure JavaScript perspective.

function Tool() {
    this.test = function(param) { // create a new method called 'test' on our Tool object
        console.log(param); // output the value of 'param' to the console
    }
}
var tool = new Tool(); // Create a new Tool
var func = tool['test']; // assign the 'test' method of 'tool' to the 'func' variable

func("input"); // run the 'func' function with "input" as the parameter

3 Comments

Not sure how my answer wasn't full? :)
@Drewdiddy611 You're answer was great – I just choose to abstract away the actual functionality of the code and explain it from a pure JavaScript OOP perspective. After reading your answer, I thought mine still added to the conversation to I decided to post anyway, in case someone finds it useful down the line at some point.
and simpler even more you could use an anonymous function via the function operator: var tool = new function() { this.test = function(param) { console.log(param); } }; P.S. @ryanbrill I was trying to really get some feedback about my answer, I hope I didn't come off as being rude. :) Your answer was helpful and I upvoted it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.