// Example of invalid key namesconsttrainSchedule={// Invalid because of the space between words.platformnum:10,// Expressions cannot be keys.40-10+2:30,// A + sign is invalid unless it is enclosed in quotations.+compartment:'C'}
constorigNum=8;constorigObj={color:"blue"};constchangeItUp=(num,obj)=>{num=7;obj.color="red";};changeItUp(origNum,origObj);// Will output 8 since integers are passed by value.console.log(origNum);// Will output 'red' since objects are passed// by reference and are therefore mutable.console.log(origObj.color);
// A factory function that accepts 'name',// 'age', and 'breed' parameters to return// a customized dog object.constdogFactory=(name,age,breed)=>{return{name:name,age:age,breed:breed,bark(){console.log("Woof!");},};};
Object methods
constengine={// method shorthand, with one argumentstart(adverb){console.log(`The engine starts up ${adverb}...`);},// anonymous arrow function expression with no argumentssputter:()=>{console.log("The engine sputters...");},};engine.start("noisily");engine.sputter();
Getters and setters
constmyCat={_name:"Dottie",getname(){returnthis._name;},setname(newName){this._name=newName;},};// Reference invokes the getterconsole.log(myCat.name);// Assignment invokes the settermyCat.name="Yankee";