for in / for of
let arr = [1,2,3,"fsd"]
for(var index of arr) {
console.log(index);
}
for(var index in arr) {
console.log(index);
}
let obj = {
id:"001",
name: "lisi",
age: 18
}
for(var key in obj) {
console.log(key)
}
let set = new Set();
set.add(1).add(2).add(3).add(3);
console.log(set)
// for(let k of set) {
// console.log(k);
// }
for(let k in set) {
console.log(k)
}
let m = new Map();
m.set("name","lisi").set("age",18).set({faver: "ping"}, "爱好");
console.log(m);
// for(let k of m) {
// console.log(k)
// }
for(let k in m) {
console.log(key)
}
// 针对普通数组, for of 取到的对应的值; for in 取到对应的索引;
// 针对普通对象, 不能使用for of ; for in 取到对应的属性;
//set 类似数组, for of 取到对应的值; 不能使用for in ;
//map 类似对象, for of 取到一个映射的数组; 不能使用for in;
let obj2 = {
"fsdf":"fsdf",
1:"a",
fn: () => {
},
false: "fsdf",
'{a:2}': {fa:"23"}
}
console.log(obj2)
//结构赋值
//默认值
function add(a,b=100) {
return a + b;
}
console.log(add(1,false));
网友评论