TGarrett
TGarrett

Reputation: 552

Cleaner way to see if function is defined

Is there an easier way to check if a javascript function has already been called and is active besides what I have below, or is what I have below already optimized...

    var isDefined = function(func) {
       if(func !== undefined)
       {
          return true;
       }
       else
       {
          return false;
       }
     }

Upvotes: 0

Views: 53

Answers (2)

Katana314
Katana314

Reputation: 8610

JavaScript does nothing to retain memory of a function having already been called. You can put a Boolean var flag outside of it, set it to true inside, and ensure its scope doesn't leak by putting it inside an IIFE, which may give you what you want.

If what you're doing is related to click/event listening, you may want to use something like JQuery's $.once(), which adds an event listener that removes itself after occurring once.

Upvotes: 2

ferdynator
ferdynator

Reputation: 6410

var isDefined = function(func) {
     return (func !== undefined);
}

Upvotes: -1

Related Questions