Reputation: 204
In Javascript, I want to check if an attribut (or object) is a function or not.
My Present code:
if (this.execute != null)
{
this.execute();
}
Error: Object doesn't support this property or method.
i want to do something like
if (this.execute != null and isExecutableFunction(this.execute)== true )
{
this.execute();
}
Is there anyway in Javascript to check if an attribute (or object ) is a function or not
Any help shall be appreciated.
Thanks.
Upvotes: 4
Views: 5254
Reputation: 11351
Try this. Works fine in jsfiddle. I changed this to t since I wasn't working inside a function. Try it with line 2 both remarked and unremarked.
var t= new Object;
//t.execute = function() { alert('executing'); };
if(t.execute)
t.execute();
else
//code here when execute fails
alert('Function could not execute');
Upvotes: -1
Reputation: 50039
You could use typeof
var func = function(){};
alert(typeof func)
If you put this in your firebug console and run it, you get function
as the return.
So you just need to check for it.
Upvotes: 0