Skip to content

Instantly share code, notes, and snippets.

View behnamazimi's full-sized avatar

Behnam behnamazimi

View GitHub Profile
const data = ["A", "B", "C"];
for (let item of data) {
console.log(item)
} //o: A B C
const data = {
items: ["A", "B", "C"],
pointerIndex: 0,
next() {
if (this.pointerIndex < this.items.length) // if not done
return { value: this.items[this.pointerIndex++], done: false }
else // if done
return { value: undefined, done: true };
}
}
let dataElement = data.next();
while (!dataElement.done) {
console.log(dataElement.value)
dataElement = data.next();
} //o: A B C
console.log(data.next()) //o: {value: "A", done: false}
console.log(data.next()) //o: {value: "B", done: false}
console.log(data.next()) //o: {value: "C", done: false}
console.log(data.next()) //o: {value: undefined, done: true}
const data = {
items: ["A", "B", "C"],
pointerIndex: 0,
next() {
if (this.pointerIndex < this.items.length) // if done
return { value: this.items[this.pointerIndex++], done: false }
else // if not done
return { value: undefined, done: true };
}
}
const symblOne = Symbol("Description");
const symblTwo = Symbol("Description");
symblOne === symblTwo //o: false
const symblOne = Symbol();
const myObject = {
[symblOne]: "My Data",
//...
}
console.log(myObject[symblOne]) //o: "My Data"
const myObject = {
[Symbol()]: "My Data",
//...
}
console.log(myObject) //o: {Symbol(): "My Data"}
const myObject = {
[Symbol("myKey")]: "My Data",
//...
}
console.log(Object.getOwnPropertySymbols(myObject)); //o: [Symbol(myKey)]
const symbl = Object.getOwnPropertySymbols(myObject)[0]
console.log(myObject[symbl]) //o: "My Data"
const data = {
items: ["A", "B", "C"],
pointerIndex: 0,
[Symbol.iterator]: function () {
if (this.pointerIndex < this.items.length) // if done
return { value: this.items[this.pointerIndex++], done: false }
else // if not done
return { value: undefined, done: true };
}
}