网站首页 > 教程文章 正文
近日,于闲暇之时,我沉浸于 JavaScript 之经典著作犀牛书,当阅览至循环语句部分,顿觉有必要予以记录。
for/of 与 for/in 乃是工作中屡被运用的语句,for/of 是 ES6 新增的,而 for/in 是JavaScript从一开始就有的,以下将记录下它们之间的主要差异。
主要区别
特性 | for/of | for/in |
遍历对象 | 可迭代对象的值 | 任意对象 |
用途 | 遍历数组、字符串等可迭代对象的值 | 遍历对象属性名 |
原型链属性 | 只遍历对象自身的值 | 会遍历继承的可枚举属性 |
适用对象 | 数组、字符串、Map、Set 等可迭代对象 | 普通对象 |
示例比较
1.遍历数组
const arr = ['a', 'b', 'c'];
// for...in 遍历索引/键名
for (let index in arr) {
console.log(index); // 输出: 0, 1, 2
}
// for...of 遍历值
for (let value of arr) {
console.log(value); // 输出: 'a', 'b', 'c'
}
2.遍历对象
const obj = { a: 1, b: 2, c: 3 };
// for...in 可以遍历对象属性
for (let key in obj) {
console.log(key); // 输出: 'a', 'b', 'c'
}
// for...of 不能直接用于普通对象
// 会抛出 TypeError: obj is not iterable
try {
for (let value of obj) {
console.log(value);
}
} catch (e) {
console.log(e.message);
}
3.原型链上的属性
function Person() {
this.name = 'Alice';
}
Person.prototype.age = 25;
const person = new Person();
// for...in 会遍历原型链上的可枚举属性
for (let prop in person) {
console.log(prop); // 输出: 'name', 'age'
}
// 可以使用 hasOwnProperty 过滤
for (let prop in person) {
if (person.hasOwnProperty(prop)) {
console.log(prop); // 输出: 'name'
}
}
4.遍历字符串
const str = "hello";
// for...in 遍历索引
for (let index in str) {
console.log(index); // 输出: 0, 1, 2, 3, 4
}
// for...of 遍历字符
for (let char of str) {
console.log(char); // 输出: 'h', 'e', 'l', 'l', 'o'
}
5.遍历其他可迭代对象
const set = new Set([1, 2, 3]);
// for...in 不适用于 Set
for (let prop in set) {
console.log(prop); // 不输出任何内容
}
// for...of 可以遍历 Set 的值
for (let value of set) {
console.log(value); // 输出: 1, 2, 3
}
何时使用
- 使用 for/in 当你需要遍历对象的属性名
- 使用 for/of 当你需要遍历数组、字符串或其他可迭代对象的值
在 ES6 及更高版本中,对于数组遍历,通常推荐使用 for/of 而不是 for/in,因为它直接提供值而不是索引,并且不会意外遍历到数组的非数字属性。
猜你喜欢
- 2025-05-25 China committed to continuing contributions to global health: delegation
- 2025-05-25 Chinese, German experts urge cooperation during Eurasia relations seminar
- 2025-05-25 Peace of paramount importance for region
- 2025-05-25 after和in用法解析
- 2025-05-25 China's top diplomat to chair third China-Pacific Island countries foreign ministers' meeting
- 2025-05-25 CICEE Draws Participation of Global Construction Machinery Traders
- 2025-05-25 China, Indonesia should carry forward Bandung Spirit, says Premier Li
- 2025-05-25 常用的虚拟语气句型if it wasn't/weren't for用法解析
- 2025-05-25 China is steadfast practitioner of 'mutually beneficial' principle
- 2025-05-25 Highlights of Xi's remarks during his visit to Cambodia
- 05-25干货 | 一步步部署 Flask 应用
- 05-25别再去找Docker命令了,你要的常用的全都在这
- 05-25如果您删除Windows11上的“Program Files”文件夹会发生什么?
- 05-25家用nas最常用的docker容器及部署方法
- 05-25你好 dotnet run file, 再见 csproj
- 05-25China committed to continuing contributions to global health: delegation
- 05-25Chinese, German experts urge cooperation during Eurasia relations seminar
- 05-25Peace of paramount importance for region
- 最近发表
-
- 干货 | 一步步部署 Flask 应用
- 别再去找Docker命令了,你要的常用的全都在这
- 如果您删除Windows11上的“Program Files”文件夹会发生什么?
- 家用nas最常用的docker容器及部署方法
- 你好 dotnet run file, 再见 csproj
- China committed to continuing contributions to global health: delegation
- Chinese, German experts urge cooperation during Eurasia relations seminar
- Peace of paramount importance for region
- after和in用法解析
- China's top diplomat to chair third China-Pacific Island countries foreign ministers' meeting
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)