最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Tell if a Javascript function is defined by looking at self[name] - is this a good way? - Stack Overflow

programmeradmin1浏览0评论

This is a follow up question to This Question.

I like (and understand) the solution there. However, in the code I am working in, another way to solve the same problem is used:

function exist(sFN) {
    if(self[sFN]) return true;
    return false;
}

It seems to work fine, although I don't understand how. Does it work? How? What are minuses of this approach? Should I switch to solution from the other question?

This is a follow up question to This Question.

I like (and understand) the solution there. However, in the code I am working in, another way to solve the same problem is used:

function exist(sFN) {
    if(self[sFN]) return true;
    return false;
}

It seems to work fine, although I don't understand how. Does it work? How? What are minuses of this approach? Should I switch to solution from the other question?

Share Improve this question edited May 23, 2017 at 12:25 CommunityBot 11 silver badge asked Sep 17, 2008 at 22:52 buti-oxabuti-oxa 11.4k5 gold badges36 silver badges44 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 7

Try this:

function exist(sFN) {
 return (typeof sFN == 'function');
}

Your condition is checking the existence of the "sFN" property in the "self" object. Anything that isn't null, undefined, 0, and "" will evaluate to true.

As others have said, you can use typeof, or instanceof to see if it's actually a function.

Looking at your linked example, you should read up on the difference between ==/!= and ===/!== in javascript. Short answer: ("" == null) is true, ("" === null) is false.

just use typeof.

typeof(foobar)  // -> undefined
typeof(alert)   // -> function

You can't, however, defined a function based on typeof, because you'd need to pass an identifier which might not exist. So if you define function isfun(sym) { return typeof(sym) }, and then tried calling isfun(inexistent), your code would throw.

The fun thing about typeof is that it's an operator, not a function. So you can use it to check a symbol that's not defined without throwing.


if you assume a function in the global scope (i.e., not within a closure), you can define a function to check it as follows:

function isfun(identifier) {
  return typeof(window[identifier]) == 'function';
}

Here you pass an string for the identifier, so:

isfun('alert');   // -> true
isfun('foobar');  // -> false

closure?

Here's an example of a function defined within a closure. Here, the printed value would be false, which is wrong.

(function closure() { 
  function enclosed() {}
  print(isfun('enclosed'))
})()

FYI: There is (or was) a nice pitfall for typeof.

FF2 returns 'function' for typeof(/pattern/). FF3, IE7, and Chrome all return 'object' for the same code. (I can't verify other browsers.)

Assuming everyone that used FF2 has upgraded, you're in the clear. But, that's probably a far-fetched assumption.

You can't really wrap this in a method, but it's so simple there is really no need.

function bob()
{}

if( typeof bob == "function" )
    alert( "bob exists" );

if( typeof dave != "function" )
    alert( "dave doesn't" );

Object.prototype.toString.apply(value) === '[object Function]'

I read somewhere (here and here) that functions are properties of the window object, so you can do the following:

if (window.my_func_name) {
    my_func_name('tester!');
}

or for popups:

if (window.opener.my_func_name) {
    my_func_name('tester!');
}

A complete solution then:

function function_exists(func_name) {
    var eval_string;
    if (window.opener) {
        eval_string = 'window.opener.' + func_name;
    } else {
        eval_string = 'window.' + func_name;
    }
    return eval(eval_string + ' ? true : false');
}
发布评论

评论列表(0)

  1. 暂无评论