to access building number you need array like notation
console.log(person['building number']); //array like notation
modify values in object
person.lastName = 'ibrahim';
console.log(person);
add new property
person.age = 25;
console.log('new added value:', person.age);
deleting property
delete person.age;
checking if property exist
console.log('lastName' in person);
accessing all key and value using for in loop
for (let key in person) {
console.log(key + ":" + person[key]);
}
console.log(person);
******************* object using new keyword *********************
The regular {... } syntax allows us to create one object.But often we need to create many similar objects, like multiple users or menu items and so on.
That can be done using constructor functions and the "new" operator.
new keyword
var myDetails = new myDetails();
myDetails.age = 25;
myDetails.firstName = 'aamir';
myDetails.lastName = 'ali';
myDetails.greet = function () {
console.log("welllcome to js");
}
console.log(myDetails);
console.log(myDetails.greet);
************ constructor *********** *
Constructor functions technically are regular functions.There are two conventions though:
They are named with capital letter first.
They should be executed only with "new" operator.
Using constructor functions to create objects gives a great deal of flexibility.The constructor function may have parameters that define how to construct the object, and what to put in it.
For instance:
function User(name) {
this.name = name;
this.isAdmin = false;
}
let user = new User("Jack");
alert(user.name); // Jack
alert(user.isAdmin); // false
When a function is executed with new, it does the following steps:
A new empty object is created and assigned to this.
The function body executes.Usually it modifies this, adds new properties to it.
The value of this is returned.
add method in constructor
Of course, we can add to this not only properties, but methods as well.
function User(name) {
this.name = name;
this.sayHi = function () {
alert("My name is: " + this.name);
};
}
let john = new User("John");
john.sayHi(); // My name is: John
****** objects methos ****** *
an object property that include a function declaration is known as object method
types of object method
1) Object.freez():
changinfg the frozen object is impossible
it prevent the addition and deletion of property
also prevent changes
Object.freeze(john);
2) objet.seal():
you can not add or remove property but you can modify property
Object.seal(john);
A function which is associated with object called method
When a function is a property of an object, it becomes a method.
Optional chaining is a feature of javascript that allow you to access the properties of an object and element of an array without having to check the array and object is null or undefine first it is represent by?.operator and used to cociesly access deeply nested property without having to write long chain of if else statement
the optional chaining?.stops the evaluation if the value before?.is undefined or null and returns undefined.
**********example *********
let user = {}; // user has no address
alert(user?.address?.street); // undefined (no error)
The?.[] syntax also works, if we’d like to use brackets[] to access properties instead of dot..Similar to previous cases, it allows to safely read a property from an object that may not exist.
the destructurig assignment syntex is a javascript expression that make it possible to unpack from Array or property from object, into distinct variables.
let obj = {
name: 'ali',
age: 25,
city: {
country: 'USA',
state: 'California'
}
};
let { name, age, city } = obj;
console.log(`name is${name} and age is ${age} city is ${city.state}`);