判断js的Array和Object的实现方法

网络编程 2021-07-04 20:00www.168986.cn编程入门
狼蚁网站SEO优化长沙网络推广就为大家带来一篇判断js的Array和Object的实现方法。长沙网络推广觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧
var a = ['hello','world'];
  console.log(typeof a); // object
  console.log(a.toString()); // hello,word 字符串
  console.log(Object.prototype.toString.call(a)); //[object Array]

  var b = {'hello':'world'};
  console.log(typeof b); // object
  console.log(b.toString()); // [object Object]
  console.log(Object.prototype.toString.call(b));// [object Object]

1. Array 和 Object 的typeof 结果都是 object

2. toString() Array的结果是真的字符串,而Object的结果是固定的[object Object]

3. 通过Object.prototype.toString调用,Array的结果返回固定[object Array],Object的结果依然是[object Object]

ES2005 新方法 

console.log( Array.isArray(a) ); // true
console.log( Array.isArray(b) ); // false

以上这篇判断js的Array和Object的实现方法就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by