Photo by Stefan Cosma on Unsplash

Objects

Properties of Objects

let newObject = {
key1 : "value",
key2 : {
myKey : 14
}
myFuction : function(){
console.log(`This is how we use function/Method in an Object`) ;
}
}

Accessing Values

console.log(newObject.key1) ; //[value](//value) 
newObject.myFunction() // This is how we use function/Method in an Object

Delete Operator

let person = {
name : "John" ,
age : 18 ,
isAdmin : false ,
}
console.log(person)
//{name: "John", age: 18, isAdmin: false}
delete person.isAdminconsole.log(person)
//{name: "John", age: 18}

Accessing properties which are doesn’t exist in an Object

Objects are mutable in JavaScript

let student = {
name : "John"
}
student.name = "Harry"
console.log(student) // {name: "Harry"}

JavaScript for…in loop

let mobile = {
brand: 'Samsung',
model: 'Galaxy Note 9'
};
for (let key in mobile) {
console.log(`${key}: ${mobile[key]}`);
}

this keyword

let myObj = {
name: 'John',
sayName: function () {
console.log(this);
},
};
myObj.sayName();
//{ name: 'John', sayName: [Function: sayName] }
let newObj = {
name : "newObj" ,
sayName : function(){
console.log(this.name) ;
}
}
newObj.sayName() //newObj

JavaScript Arrow Function this Scope

const myObj = {
data: 'abc',
loggerA: () => { console.log(this.data); },
loggerB() { console.log(this.data); },
};
myObj.loggerA(); // undefined
myObj.loggerB(); // 'abc'

JavaScript factory functions

const dogFactory = (firstName, secondName) => {
return {
firstName: firstName,
secondName: secondName,
name : function() {
console.log(`${this.firstName}${this.secondName}`) ;
}
};
};

JavaScript DE Structuring

const Lists = {
students : {
...
} ,
staffs : {
...
} ,
HOD : '' ,
totalStudents : 100 ,
}
console.log(Lists.totalStudents) // 100//insteadconst { students , staffs , HOD , totalStudents } = Listsconsole.log(totalStudents) ;