Array.isArray (Array) – JavaScript 中文开发手册

[
  •   JavaScript 中文开发手册

    Array.isArray (Array) - JavaScript 中文开发手册

    Array.isArray() 用于确定传递的值是否是一个Array。

    Array.isArray([1, 2, 3]);  // true
    Array.isArray({foo: 123}); // false
    Array.isArray('foobar');   // false
    Array.isArray(undefined);  // false

    语法

    Array.isArray(obj)

    参数

    obj需要检测的值。

    返回值

    如果对象是Array,则为true; 否则为false。

    描述

    如果对象是Array ,则返回true,否则为false。

    示例

    // all following calls return true
    Array.isArray([]);
    Array.isArray([1]);
    Array.isArray(new Array());
    // Little known fact: Array.prototype itself is an array:
    Array.isArray(Array.prototype); 
    
    // all following calls return false
    Array.isArray();
    Array.isArray({});
    Array.isArray(null);
    Array.isArray(undefined);
    Array.isArray(17);
    Array.isArray('Array');
    Array.isArray(true);
    Array.isArray(false);
    Array.isArray({ __proto__: Array.prototype });

    instanceof 和 isArray

    当检测Array实例时, Array.isArray 优于 instanceof,因为Array.isArray能检测iframes.

    var iframe = document.createElement('iframe');
    document.body.appendChild(iframe);
    xArray = window.frames[window.frames.length-1].Array;
    var arr = new xArray(1,2,3); // [1,2,3]
    
    // Correctly checking for Array
    Array.isArray(arr);  // true
    // Considered harmful, because doesn't work through iframes
    arr instanceof Array; // false

    Polyfill

    假如不存在 Array.isArray(),则在其他代码之前运行下面的代码将创建该方法

    if (!Array.isArray) {
      Array.isArray = function(arg) {
        return Object.prototype.toString.call(arg) === '[object Array]';
      };
    }

    规范

    Specification

    Status

    Comment

    ECMAScript 5.1 (ECMA-262)The definition of 'Array.isArray' in that specification.

    Standard

    Initial definition. Implemented in JavaScript 1.8.5.

    ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Array.isArray' in that specification.

    Standard

    ECMAScript Latest Draft (ECMA-262)The definition of 'Array.isArray' in that specification.

    Living Standard

    浏览器兼容性

    Feature

    Chrome

    Edge

    Firefox

    Internet Explorer

    Opera

    Safari

    Basic Support

    5

    (Yes)

    4

    9

    10.5

    5

    Feature

    Android

    Chrome for Android

    Edge mobile

    Firefox for Android

    IE mobile

    Opera Android

    iOS Safari

    Basic Support

    (Yes)

    (Yes)

    (Yes)

    4

    (Yes)

    (Yes)

    (Yes)

  •   JavaScript 中文开发手册
    ]
    转载请保留页面地址:https://www.breakyizhan.com/javascript/32539.html
    原文地址:https://www.cnblogs.com/breakyizhan/p/13295728.html