classObjectLayout{constructor(){this.firstName="Larry";//property}sayHi(){// Methodreturn`Hello from ${this.firstName}`;}}const object =newObjectLayout();// object.firstName is Larry;// object.sayHi() gives "Hello from Larry"
// To make an object literal:const dog ={name:"Rusty",breed:"unknown",isAlive:false,age:7}// All keys will be turned into strings!// To retrieve a value:
dog.age;//7
dog["age"];//7//updating values
dog.breed="mutt";
dog["age"]=8;
1)ObjectLiteralconst obj ={name:"Ram",age:"20"}2)ConstructorFunction or ES6-Classconst a =newPerson("Ram","25")//given Person is an existing function or class3)Object.create method
const ram =Object.create(Person);//given person is an existing object
const person ={name:'Anthony',age:32,city:'Los Angeles',occupation:'Software Developer',skills:['React','JavaScript','HTML','CSS']}//Use Template Literal to also log a message to the consoleconst message =`Hi, I'm ${person.name}. I am ${person.age} years old. I live in ${person.city}. I am a ${person.occupation}.`;console.log(message);
//create an obect of a scary housekeepervar houseKeeper ={//the following are all propertiesname:"Moira O'Hara",//creating variables and assigningexperienceInYears:9,everBeenFired:false,priorJobs:['Grand Budapest Hotel','The Overlook','Pinewood Hotel','Slovakian Hostel'],dateHired:newDate('01/13/2022'),currentEmployee:true,dateOfTermination:newDate(0),reasonForTermination:'',};//using dot notation to edit object
houseKeeper.everBeenFired=true;
houseKeeper.currentEmployee=false;
houseKeeper.dateOfTermination=newDate('10/3/2022');
houseKeeper.reasonForTermination=['anger issues','violation of customer privacy']//using dot notation to access objectconsole.log("Date of Hire : ", houseKeeper.dateHired)console.log("House Keeper : ", houseKeeper.name)console.log("Current Employee? : ", houseKeeper.currentEmployee)console.log("Date of Termination : ", houseKeeper.dateOfTermination)console.log("Reason for Termination : ", houseKeeper.reasonForTermination)
let voleur ={action:()=>console.log('Coup de dague fatal'),crie:('pour la horde!!!!'),coupfatal:()=>console.log('coup dans le dos')}
voleur.action();
voleur.coupfatal();console.log(voleur.crie);
const person ={name:'Nick'};
person.name='John'// this will work ! person variable is not completely reassigned, but mutatedconsole.log(person.name)// "John"
person ="Sandra"// raises an error, because reassignment is not allowed with const declared variables