writeCode() { console.log( this._name + ' is coding in ' + this._programmingLanguage + '.' ); } }
let bob = new Person('Bob'); bob.walk(); //Bob is walking. //the usage of getter and setter console.log(bob.name); //Bob bob.name = "Aob"; console.log(bob.name); //Aob
let cory = new Programmer('Cory', 'JavaScript'); cory.walk(); //Cory is walking. cory.writeCode(); //Cory is coding in JavaScript. console.log(cory.name); //Cory
Pay attention to the difference between for-of loop and for-in loop
The following example shows the difference between a for...of loop and a for...in loop. While for...in iterates over property names, for...of iterates over property values:
1 2 3 4 5 6 7 8 9 10
const arr = [3, 5, 7]; arr.foo = 'hello';
for (let i in arr) { console.log(i); // logs "0", "1", "2", "foo" }
for (let i of arr) { console.log(i); // logs 3, 5, 7 }