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.
Use the typeof operator to check if an object contains a function, e.g. typeof obj. sum === 'function' . The typeof operator returns a string that indicates the type of the value.
Object is a function, but not every object is a function.
Use the typeof operator to get the type of an object or variable in JavaScript. The typeof operator also returns the object type created with the "new" keyword. As you can see in the above example, the typeof operator returns different types for a literal string and a string object.
You can call it like this:
testObj.callableFunction && testObj.callableFunction();
or in details:
if (typeof testObj.callableFunction == 'function') { testObj.callableFunction(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With