Last active
January 19, 2017 18:31
-
-
Save olange/2aa1abe1001c92bde78be1c84d7f1261 to your computer and use it in GitHub Desktop.
Reliably detect types of Javascript objects and primitive values, easing polymorphic programming in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Helper module to reliably find the types of Javascript objects | |
# and primitive values, easing polymorphic programming. | |
# | |
# See also: | |
# * http://javascript.info/tutorial/type-detection | |
# * https://gist.github.com/olange/514325f6fe3d89610d224d731a2def9e | |
exp = module.exports = {} | |
# | |
# Returns the value of the hidden [[Class]] property of the given | |
# object; this property exists in all JavaScript native objects. | |
# | |
# Returns following values: | |
# * `"[object Array]"` for an instance of an `Array` | |
# * `"[object Date]"` for a Date object | |
# * `"[object Boolean]"` for a boolean primitive value | |
# * `"[object String]"` for a String object | |
# * `"[object Number]"` for an int or float primitive value | |
# * `"[object Null]"` for `null` and `undefined` | |
# | |
# Beware: works for `null` and `undefined` on Node.js, but | |
# would return the `window` object when running in a browser. | |
# | |
exp.classOf = (obj) -> | |
{}.toString.call( obj) | |
# eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment