Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // More exact variable type is possible only by prototype toString method:
- var realTypeOf = function (object) {
- // typeStr is always in form: '[object MoreSpecificName]'
- var typeStr = {}.toString.apply(object);
- // let's return only the last part 'MoreSpecificName'
- var result = typeStr.substr(8, typeStr.length - 9);
- if (result == "Number") {
- if (!isFinite(object)) {
- result += ".Inifinite";
- } else if (isNaN(object)) {
- result += ".NaN";
- } else if (object.toString.indexOf('.') > -1) { // Math.round(object) !== object
- result += ".Float";
- } else {
- result += ".Int";
- }
- }
- return result;
- };
- // now is returning type much better:
- console.log(
- realTypeOf({}), // Object
- realTypeOf(1), // Number.Int
- realTypeOf(.5), // Number.Float
- realTypeOf(NaN), // Number.NaN
- realTypeOf(Infinity), // Number.Inifinite
- realTypeOf(""), // String
- realTypeOf(false), // Boolean
- realTypeOf([]), // Array
- realTypeOf(new Date()), // Date
- realTypeOf(null), // Null
- realTypeOf(undefined), // Undefined
- realTypeOf(function(){})// Function
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement