How to check if an object has a function? (DoJo)

antonpug picture antonpug · Feb 19, 2013 · Viewed 40.1k times · Source
var testObj = this.getView();

How can I check with DoJo (or just native JS) if testObj has callableFunction before I actually try to call callableFunction() and fail if it isn't there? I would prefer a native-DoJo solution as I need this to work on all browsers.

Answer

dfsq picture dfsq · Feb 19, 2013

You can call it like this:

testObj.callableFunction && testObj.callableFunction();

or in details:

if (typeof testObj.callableFunction == 'function') {
    testObj.callableFunction();
}