为什么要用Object.prototype.toString而不是Function.prototype.toString或者其它?这是和他们的toString解释方式有关系的。下面是ECMA中对Object.prototype.toString的解释:
Object.prototype.toString( )When the toString method is called, the following steps are taken: 1. Get the [[Class]] property of this object. 2. Compute a string value by concatenating the three strings “[object “, Result (1), and “]”. 3. Return Result (2)
The [[Class]] property of the newly constructed object is set to “Array”.
function isArray(o) { return Object.prototype.toString.call(o) === '[object Array]'; }
var arr = []; return arr instanceof Array;