JavaScript 中的 for 循环语句相信大家都已经快用厌了,现在有好多文章都在讲怎么减少代码中的 for 循环语句,但是,你又不得不承认它们真的很有用。今天,我来总结一下前端 JavaScript 中三种 for 循环语句。
for
这大概是应用最广的循环语句了吧,简单实用,且大多数时候性能还是在线的,唯一的缺点大概就是太普通,没有特色,导致很多人现在不愿用它。
const array = [4, 7, 9, 2, 6];
for (let index = 0; index < array.length; index++) {
const element = array[index];
console.log(element);
}
// 4, 7, 9, 2, 6
for…in
for...in
语句可以以任意顺序遍历一个对象的除 Symbol 以外的可枚举属性。
const temp = {
name: "temp"};
function Apple() {
this.color = 'red';
}
Apple.prototype = temp;
const obj =