使用for of遍历对象
const obj={
a:1,
b:2,
}
for (const key of Object.keys(obj)) {
console.log("isme for of", obj[key]);
}
对象转换为可迭代
obj[Symbol.iterator] = [][Symbol.iterator]
自己写一个迭代器
const iterator=(arr)=>{
let index=1;
return {
next:()=>{
value:arr[index++],
done:index>=arr.length
}
}
# 如何让 (a == 1 && a == 2 && a==3) 结果为 true
Object.defineProperty(window,'a',{
get:function(){
this.value=this.value ? ++this.value :1;
return this.value;
}
});
let a=[1,2,3];
a.toString=arr.shift;
console.log(a==1&&a==2&&a==3)
手写代码让一个对象取值都是123
const obj={a:1,b:2,c:3};
const foo=(obj)=>{
const keys=Object.keys(obj);
keys.forEach((key)=>{
Object.defineProperty(obj,key,{
get: function(){
return 123
}
})
});
}
foo(obj);
console.log(obj.a);