Skip to content

Instantly share code, notes, and snippets.

@Maxondria
Created May 5, 2019 19:34
Show Gist options
  • Select an option

  • Save Maxondria/2929040bf251170f276ab5f166cabe78 to your computer and use it in GitHub Desktop.

Select an option

Save Maxondria/2929040bf251170f276ab5f166cabe78 to your computer and use it in GitHub Desktop.
Enumerable vs iterable
//enumerable vs iterable
//enumerable is a keyword in every object...and it's by default true
//this means that if you loop through the object using a for...in, it all properties
//will be looped through except where a property' enumerable is defined as false.
//see below
let log = console.log;
let names =["Maxon","Moureen", "Kaihura-nkuba"];
names.elf = "Lindolf";
Object.defineProperty(names, "ent",{ name: "Treebard", enumerable: true});
//by setting enumerable to false above, we cant find this if we loop through
//setting enumerable to true, we can see it
log(names)
for (prop in names){
log(prop); //"0" ,"1" ,"2" ,"elf"
}
//Example 2
let middleEarth = {
'towns': ["Antact", "Tica"],
'races': ["Muk", "Ama", "Pendo"]
}
middleEarth.creator = "A.R.R Beader";
Object.defineProperty(middleEarth, "age",{ value: "3rd", enumerable: true});
for (prop in middleEarth){
log(prop); //"0" ,"1" ,"2" ,"elf"
}
//objeccts by default are not iterable
//This means that if we try to loop through 'middleEarth',fails because
//it is an object
//looping through names however returns only those that were not created by the prototype... making these below
//iterable
//Enumerable values are not iterable
for (let p of names){
log(p);//maxon, moreen, Kaihura-nkuba
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment