JavaScript For Of

วนลูป For Of

JavaScript for of ใบประกาศวนลูปผ่านค่าของวัตถุที่สามารถวิเคราะห์ได้

มันอนุญาตให้คุณวนลูปผ่านโครงสร้างข้อมูลที่สามารถวิเคราะห์ได้ อย่างเช่น ตัวแปรแบบลิสต์ ตัวแปรข้อความ ตัวแปรแผนภูมิ ตัวแปรลิสต์ของโหนด และอื่นๆ

ภาษา

for (variable of iterable) {
  // block รหัสที่ต้องทำงาน
}

variable - สำหรับการวิเคราะห์ทุกครั้ง ค่าของคุณสมบัติต่อไปจะถูกแบ่งให้กับตัวแปร ตัวแปรสามารถประกาศด้วย const หรือ let หรือ var

iterable - มีคุณสมบัติสามารถวิเคราะห์ได้

浏览器支持

For/of Browser Support

Added to JavaScript (ES6) in 2015

Safari 7 is the first browser to support for of: IE Firefox Safari Opera
Chrome 38 Edge 12 Firefox 51 Safari 7 Opera 25
October 2014 July 2015 October 2016 October 2013 October 2014

Internet Explorer does not support For/of

Traverse Array

Example

const cars = ["BMW", "Volvo", "Mini"];
let text = "";
for (let x of cars) {
  text += x;
}

Try It Yourself

Traverse String

Example

let language = "JavaScript";
let text = "";
for (let x of language) {
text += x;
}

Try It Yourself

While Loop

We will explain in the next chapter while Loop and do/while Loop.