Skip to content

Instantly share code, notes, and snippets.

@fuchao2012
Created March 27, 2017 09:08
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 fuchao2012/59c911d808bec6edf4e046c14c17766a to your computer and use it in GitHub Desktop.
Save fuchao2012/59c911d808bec6edf4e046c14c17766a to your computer and use it in GitHub Desktop.
typeof
//Thanks to https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
// typeof now
// Undefined => "undefined"
// Null => "object" *** fu*king mistake here ***
// Boolean => "boolean"
// Number => "number"
// String => "string"
// Object(uncallable) => "object"
// Array => "object"
// Object(callable) => "function"?
// what about instanceof ? jilei!!
new Date instanceof Date; //true
Math instanceof Math //TypeError
//From ES5 Object.prototype.toString returns [object [[Class]] ]
// diff with typeof
window.toType = function(obj){
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
// test new function above
toType({a: 4}); //"object"
toType([1, 2, 3]); //"array"
(function() {console.log(toType(arguments))})(); //arguments
toType(new ReferenceError); //"error"
toType(new Date); //"date"
toType(/a-z/); //"regexp"
toType(Math); //"math"
toType(JSON); //"json"
toType(new Number(4)); //"number"
toType(new String("abc")); //"string"
toType(new Boolean(true)); //"boolean"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment