Benoit
Benoit

Reputation: 713

How to check if a function is already defined?

How to check if a function is already defined ?

Upvotes: 37

Views: 56768

Answers (10)

Sandwich
Sandwich

Reputation: 2289

Javascript functions that check if a function exists.

With jQuery.isFunction() you may test a parameter to check if it is (a) defined and (b) is of type "function." Since you asked for jQuery, this function will tickle your fancy.

jQuery.isFunction(YourFunction)

If you wish not to use jQuery for whatever reason, here's a barebones function based on code from Idealog that will check if the variable is of type function.

function isFunction(fn){
    return typeof fn === 'function'
}

Sometimes you already know it's a function and for the sake of optimization find no reason to recheck it's type, in this case, here's function that simply checks if the variable [possibly a function] is defined

function isDefined(foo){
    return typeof(foo) !== 'undefined'
}

How to use these functions

Using jQuery:

function foo(){}
if(jQuery.isFunction(foo)) alert('This is a function');

With either of the non-jQuery Javascript functions provided above. Depending on the context of usage, these functions may, or may not be reliable. See more below

function foo(){}
if(isFunction(foo)) alert('is of type function');
if(isDefined(foo)) alert('if this is a function, it is defined');

Check both undefined and using jQuery.isFunction

if (typeof myfunc !== 'undefined' && $.isFunction(myfunc)) {
    //do something
}

Source

Is jQuery.isFunction() superior?

According to Kyle Florence jQuery.isFunction() it could superior in some situations. Particularly useful in some edge cases when using jQuery methods, see his explanation.

In certain situations in some browsers, things are incorrectly returned as the "function" type, or things that are in fact functions are returned as another type. There are several test cases you can see here: https://github.com/jquery/jque...

One example:

var obj = document.createElement("object");

// Firefox says this is a function typeof obj; // => "function"

Keep in mind these are mostly edge cases, but the reason $.isFunction was made was simply to be positive about something being a function (which can be quite important for the jQuery library itself, maybe not so much for your code).

Thanks patrick dw for pointing out Kyles Article. (Patrick DW deleted his account)

From jQuery.com

Note: As of jQuery 1.3, functions provided by the browser like alert() and DOM element methods like getAttribute() are not guaranteed to be detected as functions in browsers such as Internet Explorer.

Upvotes: 53

George C
George C

Reputation: 31

typeof return string,so you can use the JavaScript typeof operator to find the type of a JavaScript variable.

if ( typeof(youerFunctionName) === 'undefined' )
{
    console.log('undefined');
}

Upvotes: 3

spacebiker
spacebiker

Reputation: 3875

This is what I use to check if a function is already defined:

if ( typeof(myFunc()) === 'undefined' )
{
    console.log('myFunc undefined');
}
else 
{
    console.log('myFunc defined');
}

Upvotes: 0

Muhammad Tahir
Muhammad Tahir

Reputation: 2485

try this one

/*********************************/

if(typeof(callback) == 'function')

/*********************************/

here is a working example

Upvotes: 1

jimmystormig
jimmystormig

Reputation: 10802

function test(){}

if(typeof test  != "undefined")
    // function is allready defined

Upvotes: 0

Richard Dalton
Richard Dalton

Reputation: 35803

if (typeof(functionName) == 'function') {
}

.

Upvotes: 7

Peter C
Peter C

Reputation: 6307

Like so:

if (typeof myFunc != 'undefined') {
    // Assign myFunc
}

Don't just test it against undefined, which is not a constant and can be reassigned.

Upvotes: 17

Herms
Herms

Reputation: 38868

Say your function is called func:

if (func) {
  // Function is already defined (or at least something is defined there)
} else {
  // Function is not defined
}

Upvotes: 1

RDL
RDL

Reputation: 7961

if ([function] != undefined) {
  [do stuff]
}

You can also use jQuery's isFunction() to check it.

Upvotes: 1

user142019
user142019

Reputation:

if (myFunc !== undefined) {
  // myFunc is defined
  foo();
}

or

if (myFunc === undefined) {
  // myFunc is not defined
  qux();
}

Upvotes: 2

Related Questions