for in——遍历对象的属性名
for of ——遍历数组的值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
http-equiv="X-UA-Compatible"
content="IE=edge"
/>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Document</title>
</head>
<body>
<script>
// for in : 遍历对象的属性名: for(let key in object){}
// for of : 遍历数组的值
var names = ['mike', 'lucy', 'lily', 'john']
var lists = { name: 'mike', age: 18 }
// 对比数组的forEach
// 1. 如果是数组类型, 都可以, 挑你喜欢的
// 2. 如果是 伪数组 , 只能用 for..of, 因为伪数组没有forEach
for (let value of names) {
console.log('value', value) //value mike value lucy value lily value john
}
for (let k in lists) {
console.log('k', k); //k name k age
}
function show () {
console.log('arguments:', arguments)
// 伪数组类型, 原型中有没有 forEach 就不能用
// 用for..of
for (let value of arguments) {
console.log(value)
}
// 如果就想用forEach: 可以从数组中借过来用
arguments.forEach = Array.prototype.forEach
arguments.forEach(value => console.log(value))
// 把arguments原型改成数组
Object.setPrototypeOf(arguments, Array.prototype)
}
show(11, 22, 33, 44, 55, 66, 77, 88)
</script>
</body>
</html>