JS Objects
In this page:
const objectName = {
property: value,
method() {
// body
}
};
objectName.property;
objectName["property"];
Object Literals
Object literal घुँघराले कोष्ठकों {} से बनता है, जिनमें key-value जोड़ियाँ होती हैं जहाँ हर key एक property का नाम बताती है और उसका value किसी भी type का हो सकता है, जिनमें numbers, strings, arrays या अन्य objects भी शामिल हैं।
उदाहरण: Object Literals
// Declare the constant `user`; its value is built below
// Declare the constant `user`; its value is built below
const user = {
name: "Priya",
age: 28,
};
// Print `user` to the console
// Print `user` to the console
console.log(user);
Properties और Methods
Property object पर store किया हुआ value है, जबकि method एक property के रूप में store किया हुआ function है, जो object को उसके data के अलावा अपना खुद का behavior देता है।
उदाहरण: Properties and Methods
// Declare the constant `user`; its value is built below
// Declare the constant `user`; its value is built below
const user = {
name: "Rahul",
// Define the method `getSummary` with no parameters
// Define the method `getSummary` with no parameters
getSummary() {
// Return ``Name: ${this.name}`` from this function
// Return ``Name: ${this.name}`` from this function
return `Name: ${this.name}`;
},
};
// Print `user.getSummary()` to the console
// Print `user.getSummary()` to the console
console.log(user.getSummary());
Properties को Access करना
Properties को सरल निश्चित नामों के लिए dot notation, object.property, से या bracket notation, object[property], से access किया जा सकता है, जो तब ज़रूरी है जब property का नाम किसी variable में store हो या उसमें विशेष characters हों।
उदाहरण: Accessing Properties
// Declare the constant `user`, set to `{ name: "Sam" }`
// Declare the constant `user`, set to `{ name: "Sam" }`
const user = { name: "Sam" };
// Print `user.name` to the console
// Print `user.name` to the console
console.log(user.name);
// Declare the constant `key`, set to "name"
// Declare the constant `key`, set to "name"
const key = "name";
// Print `user[key]` to the console
// Print `user[key]` to the console
console.log(user[key]);
this Keyword
किसी सामान्य method के अंदर, this उस object की ओर संकेत करता है जिस पर method बुलाया गया था, जिससे method उसी object की अन्य properties को object का नाम स्पष्ट रूप से लिखे बिना access कर पाता है।
उदाहरण: The this Keyword
// Declare the constant `user`; its value is built below
// Declare the constant `user`; its value is built below
const user = {
name: "Alex",
// Define the method `greet` with no parameters
// Define the method `greet` with no parameters
greet() {
// Print `Hi, I'm ${this.name}` to the console
// Print `Hi, I'm ${this.name}` to the console
console.log(`Hi, I'm ${this.name}`);
},
};
// Call `user.greet()`
// Call `user.greet()`
user.greet();
Object Destructuring
Object destructuring किसी object से विशिष्ट properties को सीधे उनके अपने स्वतंत्र variables में निकाल लेती है, एक संक्षिप्त syntax से जो आपकी ज़रूरत की हर property के लिए object का नाम दोहराने से बचाती है।
उदाहरण: Object Destructuring
// Declare the constant `user`, set to `{ name: "Nina", age: 25 }`
// Declare the constant `user`, set to `{ name: "Nina", age: 25 }`
const user = { name: "Nina", age: 25 };
// Destructure {name, age} out of `user`
// Destructure {name, age} out of `user`
const { name, age } = user;
// Print `name, age` to the console
// Print `name, age` to the console
console.log(name, age);
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: