当前位置: 首页 > news >正文

《前端面试题》- JS基础 - 伪数组

第一次听说伪数组这个概念,听到的时候还以为是说CSS的伪类呢,网上一查,这东西原来还是个很常见的家伙。

何为伪数组

伪数组有两个特点:

  1. 具有length属性,其他属性(索引)为非负整数
  2. 但是却不具备数组的方法
    也就是看起来像是数组,然而并不是…

举个例子看看

  1. 函数内部的arguments
function testArguments(a, b, c) {console.log(`arguments is array: ${Array.isArray(arguments)}`);console.log(arguments[0]);console.log(arguments[1]);console.log(arguments[2]);
}
testArguments(1,2,3);

test result
2. DOM列表

  1. JQuery选择得出的列表: $(‘div’)
    随意找一个使用jq的网站,例如:https://www.jq22.com/

如何判断真伪数组

  1. 使用instanceof 方法
  2. 使用Array.isArray()方法: 未必准确,见下文, 使用伪数组.__proto__ = Array.prototype;转换后不可用。
  3. 伪数组.constructor === Array; 适用于带constructor的场景
  4. Object.prototype.toString.call(arr) === ‘[object Array]’

尝试一下:

function testArguments(a, b, c) {console.log(`arguments is array: ${Array.isArray(arguments)}`);console.log(`arguments is array: ${arguments instanceof Array}`);console.log(`arguments is object: ${arguments instanceof Object}`);const newArguments = Array.prototype.slice.call(arguments);console.log(`newArguments is array: ${Array.isArray(newArguments)}`);console.log(`newArguments is array: ${newArguments instanceof Array}`);console.log(`newArguments is object: ${newArguments instanceof Object}`);
}testArguments(1,2,3);

如何把伪数组转换成数组

  1. Array.prototype.slice.call(); / Array.prototype.slice.apply();
  2. 原型继承: 伪数组.__proto__ = Array.prototype;arguments 无影响正常使用
  3. ES6中数组的新方法 from()
方法一: Array.prototype.slice.call(); / Array.prototype.slice.apply();
function testArguments(a, b, c) {console.log(`arguments is array: ${Array.isArray(arguments)}`);console.log(arguments[0]);console.log(arguments[1]);console.log(arguments[2]);const newArguments = Array.prototype.slice.call(arguments);console.log(`newArguments is array: ${Array.isArray(newArguments)}`);console.log(newArguments[0]);console.log(newArguments[1]);console.log(newArguments[2]);
}
testArguments(1,2,3);

方法二: 原型继承: 伪数组.__proto__ = Array.prototype;arguments 无影响正常使用
使用该方法进行转换时,Array.isArray()方法不可用来进行判断。

方法三: ES6中数组的新方法 from()

尝试一下:

function testArguments(a, b, c) {console.log(`arguments is array: ${Array.isArray(arguments)}`);console.log(`arguments is array: ${arguments instanceof Array}`);const newArguments = Array.from(arguments);console.log(`newArguments is array: ${Array.isArray(newArguments)}`);console.log(`newArguments is array: ${newArguments instanceof Array}`);
}
testArguments(1,2,3);

总结

  1. 在使用判断是否为数组时,如果无法知道数组是否可能是使用“原型继承”的方法转换得到的,就不要使用Array.isArray()方法判断对象是否为数组的方法。
  2. 在写转换方法时,由于原型继承: 伪数组.__proto__ = Array.prototype;可能存在判断失误,尽量使用Array.prototype.slice.call(); / Array.prototype.slice.apply();如果可以使用ES6,使用Array.from()方法较为简单明了。
http://www.lryc.cn/news/339887.html

相关文章:

  • TypeScript 基础语法
  • 服务器数据恢复—V7000存储raid5数据恢复案例
  • 扫雷 【搜索,哈希】
  • 如何在CentOS安装Firefox并结合内网穿透工具实现公网访问本地火狐浏览器
  • LlamaIndex 组件 - Loading
  • 再见了 wordpress !又一款简洁实用的个人博客,简单好使【文末领福利】
  • 【经典算法】LeetCode 136:只出现一次的数字(Java/C/Python3实现含注释说明,Easy)
  • ST-LINK Utility 4.6.0 下载安装及使用方法介绍
  • 【教程】cocos2dx资源加密混淆方案详解
  • 【Altium Designer 20 笔记】PCB板框
  • el-date-picker限制只能选择当前时间前/后的时间(包含日期、时、分)
  • MySQL 5.7 重置root用户密码
  • 分布式数据库Polardb-X架构及特点
  • 【spring】@Resource注解学习
  • 【leetcode面试经典150题】43. 字母异位词分组(C++)
  • 计算机网络 Cisco路由器基本配置
  • Windows Edge 兼容性问题修复:提升用户体验的关键步骤
  • Vue 3 性能飞跃:解析其性能提升的关键方面
  • MySQL 存储过程中,参数的传递主要通过以下两种方式:IN、OUT 和 INOUT
  • 修改当前Git仓库的地址、用户名、密码
  • 尚鼎环境科技诚邀您参观2024第13届生物发酵展
  • UE5 C++ 创建3DWidgete 血条 再造成伤害
  • Android 14 vold 分析(1)启动
  • 【云计算】混合云组成、应用场景、风险挑战
  • spring bean的继承和依赖
  • Swift中的字符串
  • MySQL基础-----约束详解
  • 【Unity】游戏场景添加后处理特效PostProcessing
  • STM32芯片软复位导致SRAM2的值被擦除话题
  • 【C++航海王:追寻罗杰的编程之路】异常——错误处理方式之一