callback - JavaScript - Best practice to check is function -


Before looking at the callback function I saw several ways to check the callback function.

1)

  function (callback) {... if (callback! = Null) callback (); }  

2)

  function (callback) {... if (typeof callback == 'function') callBack (); }  

3)

  function (callback) {... if (callback! == empty) callback (); }  

4)

  function (callback) {... if (callback! = Undefined) callback (); }  

5)

  function (callback) {... try {callBack (); } Hold (mistake) {}}  

6)

  function (callback) {... if (callback & type (callback) == "function") callback (); }  

7)

  function (callback) {... if (callback) callback (); }  

8)

  function (callback) {... if (typeof window [callback] === 'undefined') callback (); }  

9)

  function (callback) {... if (callback instance of function) callback (); }  

I believe there is more ...

What is the best way to check whether a given 'callback' parameter is a function or not? ? Why not use

P> and other examples?

if (callback! = Null)

Of course, why not notice there are still many non-function values ​​that pass it.

if (typeof callback == 'function')

the same You wish.

if (callback! == zero)

is not callback Undefined .

If (callback! = Undefined) does not work on

but do not see why null < / Code> is favorable (though equivalent).

try {callBack (); } Hold (mistake) {}

Uh, possibly slower in callback and swallowing mistakes. If you want the same, you should add it to one of the other tests.

If (callback and type (callback) == "function")

unnecessary, typeof may be faster than testing, if you care about performance then you want to test it, otherwise there is hardly any reason to be such a verb.

If (callback)

Sure, why not note that there are still many non-functional values ​​with integrity that Let's pass it.

if (type window [callback] === 'undefined')

not completely enough It is a different thing.

If (callback instance of function)

This should work for everything, but some extreme torrent cases, But using the typeof is safe (faster and faster).


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -