Last active
August 29, 2015 14:24
-
-
Save Jks15063/e684cb66bbde00fc4e44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj = { foo: "bar", wut: "what" }; | |
var arr = [1,2,3,4,5]; | |
obj[Symbol.iterator] = function() { | |
let keys = Object.keys(this); | |
let nextIndex = 0; | |
return { | |
next() { | |
return nextIndex < keys.length ? | |
{ value: keys[nextIndex++], done: false } : | |
{ value: void 0, done: true }; | |
} | |
}; | |
}; | |
arr[Symbol.iterator] = function() { | |
var nextIndex = 0; | |
return { | |
next() { | |
return nextIndex < this.length ? | |
{ value: this[nextIndex++], done: false } : | |
{ value: void 0, done: true }; | |
} | |
}; | |
}; | |
console.log([...obj]); | |
for(let o of obj) { | |
console.log('=', o); | |
} | |
console.log([...arr]); | |
for(let x of arr) { | |
console.log('-', x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment