思路
1 ).根据 typeof()
的返回值将数据分为2种情况
a.返回值为 'string'
'number'
'boolean'
'undefined'
'function'
(直接返回 typeof() 的返回值)
b.返回值为'object'
2 ).再将 typeof()
返回值为 'object'
的数据分为2种情况
a.null
(直接返回自身)
b.包装类
对象
数组
(再进行细分)
var a = new Number(1);
function type(target) {var ret = typeof(target);console.log(ret)var template = {//包装类、对象、数组"[object Number]" : "number - object","[object String]" : "string - object","[object Boolean]" : "boolean - object","[object Object]" : "object","[object Array]" : "array"}//找出 nullif(target === null){return null;}if(ret == "object"){var str = Object.prototype.toString.call(target);return template[str];}else{//原始值 和 functionreturn ret;}
}
console.log(type(a))
//number - object
复制代码