javascript总for of和for in的区别?

都是遍历,那有什么区别呢、?

阅读 74.2k
评论
    13 个回答
    Object.prototype.objCustom = function () {}; 
    Array.prototype.arrCustom = function () {};
    
    let iterable = [3, 5, 7];
    iterable.foo = "hello";
    
    for (let i in iterable) {
      console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
    }
    
    
    for (let i of iterable) {
      console.log(i); // logs 3, 5, 7
    }
    

    MDN