Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm wondering about snippet of code you have used #1

Closed
saadshahd opened this issue Aug 30, 2015 · 6 comments
Closed

I'm wondering about snippet of code you have used #1

saadshahd opened this issue Aug 30, 2015 · 6 comments

Comments

@saadshahd
Copy link

I'm wondering about your reasons of using this mechanism instead of the regular typeof, instanceof.
_Just I want to learn from you_, I've tried to search about differences my self but I got no thing.

@royling
Copy link

royling commented Aug 30, 2015

I got the same question. Though I've already read about your thinking about one-line module, I really did not understand why an npm module is required for this thing. IMHO, it's more simple to just put it as typeof fn === 'function' or something similar?

@saadshahd
Copy link
Author

@royling I'm not wondering about the one-line node module.

The question here why:
toString.call(x) === '[object Function]'
not:
typeof x === 'function'
nor:
x instanceof Function

@royling
Copy link

royling commented Aug 30, 2015

ok, got your point.

@sindresorhus
Copy link
Owner

This is what Lodash, Underscore and jQuery does. In modern environments typeof is good enough, but in older browsers (even Safari 8) and PhantomJS there are bugs and calling the internal [[Class]] directly Object.prototype.toString.call() works around that. I guess this is not useful in Node.js though, so I added a note 3d25982.

You should not use instanceof though as it won't work if the function comes from another realm, e.g. another frame or window, in the browser.

Read more here: http://javascript.info/tutorial/type-detection

@daryl
Copy link

daryl commented Aug 30, 2015

Interesting, thanks for clearing it up.

@saadshahd
Copy link
Author

@sindresorhus Thanks, for clarifying the differences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants