Created
July 5, 2015 01:13
-
-
Save Jks15063/ab0f2bc5546080edb547 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() { | |
let self = this; | |
let nextIndex = 0; | |
return { | |
next() { | |
return nextIndex < self.length ? | |
{ value: self[nextIndex++], done: false } : | |
{ value: void 0, done: true }; | |
} | |
//next: () => nextIndex < this.length ? { value: this[nextIndex++], done: false } : { value: void 0, done: true }; | |
}; | |
}; | |
function makeIterator(array){ | |
var nextIndex = 0; | |
return { | |
next: function() { | |
return nextIndex < array.length ? | |
{value: array[nextIndex++], done: false} : | |
{done: true}; | |
} | |
}; | |
} | |
let arrIter = arr[Symbol.iterator](); | |
console.log('spread:', [...arrIter]); | |
let arrIter2 = arr[Symbol.iterator](); | |
for(let x of arrIter2) { | |
console.log('--', x); | |
} | |
//let arrIter = makeIterator(arr); | |
//console.log('spread:', [...arrIter]); | |
//let arrIter2 = makeIterator(arr); | |
//for(let x of arrIter2) { | |
//console.log('--', x); | |
/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment