Tell me, if a Javascript function is defined looking at itself [name] is a good way?

This is the next question. This question .

I like (and understand) the solution there. However, the code I'm working in uses a different way to solve the same problem:

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

Everything seems to be fine, although I don't understand how to do it. It works? How? What are the disadvantages of this approach? Should I move on to a different issue?

+4
source share
7 answers

Your condition checks for the existence of the "sFN" property in the "self" object. Everything that is not null, undefined, 0 and "" will be evaluated as true.

As others have said, you can use typeof or instanceof to see if this is a function.

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

+6
source

Try the following:

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

just use typeof .

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

However, you cannot define a function based on typeof, because you will need to pass an identifier that may not exist. Therefore, if you define function isfun(sym) { return typeof(sym) } and then try to call isfun(inexistent) , your code will be tossed.

The most interesting thing about typeof is that it is an operator, not a function. Therefore, you can use it to check for a character that is not defined without metalization.


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

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

Here you pass the string for the id, therefore:

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

closing?

Here is an example of a function defined in a closure. Here the printed value will be false , which is incorrect.

 (function closure() { function enclosed() {} print(isfun('enclosed')) })() 
+3
source

FYI: there is (or was) a good trap for typeof.

FF2 returns a 'function' for typeof (/ pattern /). FF3, IE7 and Chrome all return an “object” for the same code. (I cannot check other browsers.)

Assuming that everyone who used FF2 has updated, you are explicitly. But perhaps this is a far-fetched assumption.

+1
source

You cannot wrap this method, but it is so simple that it is not necessary.

 function bob() {} if( typeof bob == "function" ) alert( "bob exists" ); if( typeof dave != "function" ) alert( "dave doesn't" ); 
0
source

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

0
source

I read somewhere ( here and here ) that functions are properties of a 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!'); } 

Then the complete solution:

 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'); } 
-1
source

Source: https://habr.com/ru/post/1276835/


All Articles