首页 > JS Array.isArray

JS Array.isArray

在MDN看到 Array.isArray(Array.prototype) // true
但是在Node下测试发现 Array.prototype instanceof Array // false
没明白怎么回事,求大神解答。

MDN链接:MDN Array.prototype


因为判断的方法不一样

1. instanceof

2. Array.isArray

    Object.prototype.toString = function() { 
      return "[object Array]"; 
    };
    
    Array.isArray({}); // false

ps 补充一点儿 instanceof 的内容~= ̄ω ̄=~来自这里

function instance_of(L, R) {
  //L 表示左表达式,R 表示右表达式

  var O = R.prototype;// 取 R 的显示原型
  L = L.__proto__;// 取 L 的隐式原型

  while (true) { 
    if (L === null) 
      return false; 
      
    if (O === L)// 这里重点:当 O 严格等于 L 时,返回 true 
      return true; 
      
    L = L.__proto__; 
  } 
}
【热门文章】
【热门文章】