首先看一段ECMA中对Object.prototype.toString的解释:
Object.prototype.toString( )
When the toString method is called, the following steps are taken:
- Get the [[Class]] property of this object.
- Compute a string value by concatenating the three strings “[object “, Result (1), and “]”.
- Return Result (2)
var typeOf = function() {
var toString = Object.prototype.toString;
var types = ['Object','Boolean',
'Number', 'String', 'Function',
'Array', 'Date', 'RegExp'];
var map = {};
for (var i = 0, l = types.length; i < l; i++) {
var type = types[i];
map['[object ' + type + ']'] = type.toLowerCase();
}
return function(item) {
if (item === null) return 'null';
if (item.nodeType) return 'node';
if (item.length && item.callee) return 'arguments';
if (typeof item === 'object') return map[toString.call(item)];
return typeof item;
};
}();
window.typeOf = typeOf;
网友评论