OBJECT
- Objects are variables that can store both values and functions.
- Values are stored as key:value pairs called properties.
- Functions are stored as key:function() pairs called methods
Object Properties
- Properties are key:value Pairs
- A JavaScript object is a collection of properties
- Properties can be changed, added, and deleted
- can access object properties in these ways Dot notation Bracket notation Expression
const mobile1={
brand:"samsung",
price:20000,
Storage:{
ram:"4gb",
rom:"64gb"
}
}
const mobile2={
brand:"Android",
price:22000,
Storage:{
ram:"8gb",
rom:"128gb"
}
}
const mobile3={
brand:"Apple",
price:70000,
Storage:{
ram:"8gb",
rom:"512gb"
}
}
mobile1.name="readme"
console.log(mobile1);
// let result = ("price" in mobile1);
// console.log(mobile1.brand);
// console.log(mobile2.Storage.ram);
// console.log(mobile3.price);
Output:samsung
8gb
70000
1.Change property
mobile1.price=15000
2.Add property
mobile1.name="poco"
3.delete property
mobile1.name="poco"
4.Check if property exists
let result=("price" in mobile1)
5.Nested property
const mobile1={
brand:"samsung",
price:20000,
Storage:{
ram:"4gb",
rom:"64gb"
}
}
Object Methods
- Methods are actions that can be performed on objects
- Methods are functions stored as property values
const mobile4={
brand:"samsung",
price:20000,
Storage:{
ram:"4gb",
rom:"64gb"
},
browse:function(){
console.log("5g speed browsing")
}
}
mobile4.browse()
Output:5g speed browsing
this keyword
const mobile4 = {
brand: "samsung",
price: 20000,
Storage: {
ram: "4gb",
rom: "64gb"
},
browse: function () {
// console.log("5g speed browsing")
console.log(this.brand + "5g speed browsing")
}
}
// mobile4.browse()
this.browse=mobile4.browse;
this.browse();
Output:samsung5g speed browsing
Top comments (0)