JavaScript for...in VS for...of 循环
本文比较了 JavaScript 中的 for..in
和 for..of
循环。它识别了它们之间的区别并显示了我们可以在哪里使用它们。
for..in
循环对象的属性。它在每次迭代时返回一个键,该键可用于获取该特定键的值。for..of
在可迭代对象的值上创建循环,包括内置的 Map
、Array
、Set
、String
和 TypedArray
。
两者都有一个 variable
和一个 iterable
。对于这两种情况,variable
可以是 const
、let
或 var
。在 for..of
中,iterable
是一个对象,其可迭代属性被迭代,而 for..in
的 iterable
是一个对象本身。
在 JavaScript 中使用 for..in
循环
const person = {
firstname: 'Delft',
lastname: 'Stack',
age: 30,
email: 'Delft.Stack@gmail.com'
};
for (const prop in person) console.log(`${prop}: ${person[prop]}`)
输出:
"firstname: Delft"
"lastname: Stack"
"age: 30"
"email: Delft.Stack@gmail.com"
在上面给出的代码中,for..in
正在循环一个名为 person
的对象的属性。让我们通过迭代其属性来练习使用 for..in
。
请参阅下面的示例代码。
const person = {
firstname: 'Delft',
lastname: 'Stack',
age: 30,
email: 'Delft.Stack@gmail.com'
};
function email() {
this.email = 'maofficial@gmail.com';
}
email.prototype = person;
var obj = new email();
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
console.log(`obj.${prop} = ${obj[prop]}`);
}
}
输出:
obj.email = maofficial@gmail.com
上面的代码使用 for..in
循环来检查对象是否使用 .hasOwnProperty()
来检查对象是否具有特定的属性。如果指定的属性确实是它自己的属性,则 .hasOwnProperty()
返回 true
。
在 JavaScript 中使用 for..of
循环
让我们开始用数组练习 for..of
循环。
const array = ['apple', 'banana', 'orange'];
for (const value of array) {
console.log(value);
}
输出:
apple
banana
orange
在下面的代码中,iterable
是一个字符串。
const name = 'Delft';
for (const value of name) {
console.log(value);
}
输出:
M
e
h
v
i
s
h
下面的代码实例展示了 for..of
与 Map
的用法。
const personMap =
new Map([['firstname', 'Delft'], ['lastname', 'Stack'], ['age', 30]]);
for (const entry of personMap) {
console.log(entry);
}
输出:
[ 'firstname', 'Delft' ]
[ 'lastname', 'Stack' ]
[ 'age', 30 ]
让我们使用显式实现 iterable 协议的对象来练习 for..of
。
const userDefinedfunctions = {
[Symbol.iterator]() {
return {
num: 0,
next() {
if (this.num < 10) {
return {value: this.num++, done: false};
}
return {value: undefined, done: true};
}
};
}
};
for (const value of userDefinedfunctions) console.log(value);
输出:
0
1
2
3
4
5
6
7
8
9
Symbol.iterator
用作对象的默认迭代器。这个符号由 for-of
循环使用,其中我们有 iterable
的可迭代属性被迭代。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。