Array.isArray() 和 isObject() 原生js实现

function isObject(val) {
  return val != null && typeof val === 'object' && Array.isArray(val) === false;
};


True

All of the following return true:

isObject({});
isObject(Object.create({}));
isObject(Object.create(Object.prototype));
isObject(Object.create(null));
isObject({});
isObject(new Foo);
isObject(/foo/);
False

All of the following return false:

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

var arr = new xArray(1,2,3); // [1,2,3]
Array.isArray(arr);  // true

 

posted @ 2017-09-14 10:09  Yuri_trender  阅读(16220)  评论(0编辑  收藏  举报