Varibles stores values and function
Object
Keys and Values inside curly braces{}
States and Behavior
are containers for Properties and Methods
Methods are functions.
Properties can be added,changed,deleted.
Objects are objects
Maths are objects
Dates are objects
Arrays are objects
Maps are objects
Sets are objects
RegExp are Objects
Errors are Objects
All JavaScript values, except primitives, are objects.
Example
const car = {
type: "Fiat",
model: "500",
color: "white"
};
Expplanation
car => object
Key or Properties =>type,model,color
Value or Properties Value => Fiat,500,white
Create an empty object,add the properties later
Example
// Create an Object
const person = {};
// Add Properties
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";
-Using the Object Literal
Example
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
console.log(person.age)
-Using the new keyword
Example
const person = new Object({
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
console.log(person.age)
Explanation
No need to use New Object()
for simplicity and execution speed ,use only object literal
Object.create()
creates an object from the existing objects
Example
const person = {
firstName: "John",
lastName: "Doe"
};
// Create new Object
const man = Object.create(person);
man.firstName = "Peter";
console.log(person.firstName + "and" + man.firstName)
Object FromEntries()
Create an object from key/value pairs
Example
const fruits =[
["apples",200],
["mango",180],
];
const myobj = Object.fromEntries(fruits);
console.log(myobj.mango)
Object Properties
Access object Properties in two ways
- Dot Notation
- Bracket Notation
- Expression
Dot Notation
objectName.propertyName
person.firstname
Bracket Notation
objectName["PropertyName"]
person["firstName"]
Expression
let age = person[x];
Object Methods
Object Methods are actions performed on objects
const person = {
firstName: "John",
lastName : "Doe",
age : 50,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
Explanation
'this' refers to object.
'this' refers to person object.
Changing Properties
Change the value of Property
const person = {
firstName: "John",
lastName : "Doe",
age : 50
};
person.age = 10;
console.log(person.age)
Adding new Properties
const person = {
firstname: "John",
lastname: "Doe",
age: 50,
};
person.nationality = "English";
console.log(person.nationality)
Deleting Properties
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
};
delete person.age;
console.log(person.age)
Output
Undefined
(or)
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
};
delete person["age"];
Explanation
The delete keyword deletes both the value and the property.
After deleting, the property is removed. Accessing it will return undefined.
Check if a property exists
const person = {
firstName: "John",
lastName : "Doe",
age : 50
};
let result = ("firstName" in person);
console.log(result)
Nested Objects
const myObj = {
name: "John",
age: 30,
myCars: {
car1: "Ford",
car2: "BMW",
car3: "Fiat"
}
}
console.log(myObj.myCars.car2)
Top comments (0)