0

Yes, I know there are plenty of posts on this, but I do not understand the selected answer of this post. Particularly, why is it necessary to return object?

Why wouldn't something like Object.prototype.toString.call(myFunc) be sufficient as described by MDN?

Community
  • 1
  • 1
TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114
  • @DmitriPavlutin: That isn't actually necessary, though; they both work fine. – SLaks Mar 31 '16 at 17:04
  • The point of duck-typing should be that you don't care if it's a `Function`, only whether it implements `apply` and `call`... – ssube Mar 31 '16 at 17:16

2 Answers2

1

why is it necessary to return object?

It's not necessary to do object &&, but a shortcut for falsy values that avoids the method call for values such as null. Of course, if you're looking for speed, you probably should go for typeof object == "function"

Why wouldn't something like Object.prototype.toString.call(myFunc) be sufficient?

It is sufficient.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0
return object && getClass.call(object) == '[object Function]'

This construction simply verifies if object is not null or undefined (generally falsy).
This way it's faster, because JavaScript doesn't evaluate the getClass() call on a falsy object.

Dmitri Pavlutin
  • 18,122
  • 8
  • 37
  • 41