let object ={name:"Petya",age:15};// another waylet object =newObject();
object["name"]="Petya";
object["age"]=15;// old wayvar object =newObject()
object.name="Petya";
object.age=15;
/*
An object is made of key value pairs. Keys can be strings
(which don't require quotes), array with a string, or symbols. Values can be arrays,
objects, numbers etc
*/let testSymbol =Symbol('item number 3')const obj ={item1:1,"item number 2":2,testSymbol:3,['a']:4}// Another way of creating an object:const obj =newObject();
obj.name='John'// To access values, you can use dot notation or bracket notation. // Both do the same thing, bracket notion is useful for multispace keys,// keys with dashes, or accessing values using variables> obj.item1> obj['item number 2']>let b ='item1'
obj[b]// The following would NOT work and would return undefined:
obj.b// Checking exsistence of keys in object:
obj.toString----- checks values of object and whatever object inherits from> returns true
obj.hasOwnProperty('toString')----- checks values of object only.Dothis instead of checking like:(obj.name!==undefined)> returns false// Short hand key assignment:const name ='John'const obj2 ={// this is short hand that automatically sets the key to be 'name',// and it's value to be 'John'
name,}// Constructor objects:functionObj(name, age){this.name= name
this.age= age
}const person =newObj('john',1)// adding functions, couple ways:const obj ={name:'Mike',number:4421,sayHi:()=>console.log('hi'),sayBye(){console.log('say bye')},// getter function to get values from object. Has different usecases, but the same as doing obj.numbergetgetNumber(){returnthis.number}// setter function to set values in object. Has different usecases, but the same as doing obj.number = 152setsetNumber(num){this.number= num
}}
obj.sayHi()
obj.sayBye()
obj.getNumber//Note how it's being accessed like a standard property, and not a function
obj.setNumber//Note how it's being accessed like a standard property, and not a function
//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)
const person ={firstName:'John',age:30,greet(){//methodconsole.log('Hi, I am '+this.name);},age(){//methodconsole.log('I am '+this.age+' years old');}}
person.lastName='Doe';//add property to objectconsole.log(person.lastName);// output: Doe// Calling a method
person.greet();// output: Hi, I am Doe
person.age();// output: I am 30 years old