Friday, May 12, 2017

Typeof

var typeOfArray = {
 each: function( obj, callback ) {
  var i = 0, length = obj.length;
  console.log("obj[" + length + "]=>" + obj);
  for ( ; i < length; i++ ) {
   console.log(obj[i])
   callback.call(this, obj[ i ]);
  }
 }
}
var class2type = {};
var toString = class2type.toString;
typeOfArray.each( "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ),
function(name) {
 class2type[ "[object " + name + "]" ] = name.toLowerCase();
} );
function toType(x) {
 if (x == null ) {
  return x + "";
 }
    return toString.call(x);
}
function type(x) { 
    console.log("typeof " + x + "=" + typeof x);
    console.log("Object.toString " + toType(x));
    console.log("Object type " + class2type[toType(x)]);
}
var n;
type(n);
var x = null;
type(x);
type(2);
type("2");
type("xyz");
type(true);
type({x: 1});
type(2.2);
type(["x", "y"]);
type(function(){});
type (new Boolean(true));
type(new Array(2,3));
type(new Number(2));
type(new Date());
type(new RegExp("[a-b]"));
type(new String("Hi"));
Output:
obj[10]=>Boolean,Number,String,Function,Array,Date,RegExp,Object,Error,Symbol
typeof:6 Boolean
typeof:6 Number
typeof:6 String
typeof:6 Function
typeof:6 Array
typeof:6 Date
typeof:6 RegExp
typeof:6 Object
typeof:6 Error
typeof:6 Symbol
typeof:24 typeof undefined=undefined
typeof:25 Object.toString undefined
typeof:26 Object type undefined
typeof:24 typeof null=object
typeof:25 Object.toString null
typeof:26 Object type undefined
typeof:24 typeof 2=number
typeof:25 Object.toString [object Number]
typeof:26 Object type number
typeof:24 typeof 2=string
typeof:25 Object.toString [object String]
typeof:26 Object type string
typeof:24 typeof xyz=string
typeof:25 Object.toString [object String]
typeof:26 Object type string
typeof:24 typeof true=boolean
typeof:25 Object.toString [object Boolean]
typeof:26 Object type boolean
typeof:24 typeof [object Object]=object
typeof:25 Object.toString [object Object]
typeof:26 Object type object
typeof:24 typeof 2.2=number
typeof:25 Object.toString [object Number]
typeof:26 Object type number
typeof:24 typeof x,y=object
typeof:25 Object.toString [object Array]
typeof:26 Object type array
typeof:24 typeof function (){}=function
typeof:25 Object.toString [object Function]
typeof:26 Object type function
typeof:24 typeof true=object
typeof:25 Object.toString [object Boolean]
typeof:26 Object type boolean
typeof:24 typeof 2,3=object
typeof:25 Object.toString [object Array]
typeof:26 Object type array
typeof:24 typeof 2=object
typeof:25 Object.toString [object Number]
typeof:26 Object type number
typeof:24 typeof Fri May 12 2017 20:45:31 GMT+0530 (India Standard Time)=object
typeof:25 Object.toString [object Date]
typeof:26 Object type date
typeof:24 typeof /[a-b]/=object
typeof:25 Object.toString [object RegExp]
typeof:26 Object type regexp
typeof:24 typeof Hi=object
typeof:25 Object.toString [object String]
typeof:26 Object type string
typeof:1 undefined

No comments:

Post a Comment