let obj = {
a: 'alex',
[Symbol.iterator] : function* () {
yield this.a;
}
}
Came across this code when learning about Generator. Wondering what is the term for such syntax [Symbol.iterator] where we use [] to surround object property? Can we apply it to any other property or it's only for Symbol.iterator?
Also, is it possible to make it reusable just like any other function for example as below code
let obj = {
a: 'alex',
[Symbol.iterator] : function* () {
yield this.a;
},
getA: function() {
return this.a
}
}
console.log(obj.getA())//Perhaps obj.[Symbol.iterator]()............... ?