Skip to content

Instantly share code, notes, and snippets.

@olange
Last active January 19, 2017 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olange/2aa1abe1001c92bde78be1c84d7f1261 to your computer and use it in GitHub Desktop.
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
#
# 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