JavaScript Objects in Hindi
JavaScript Objects
JavaScript में Object एक ऐसा data structure है जिसमें किसी एक entity से related multiple values को एक ही structure में store किया जा सकता है। उदाहरण के लिए किसी student, user, car, product या employee की अलग-अलग information को एक object में रखा जा सकता है।
Object में data को सामान्यतः key-value pairs के रूप में store किया जाता है। Key को property का नाम कहा जाता है और उसके सामने लिखी value उस property की value होती है।
उदाहरण के लिए किसी student की details को अलग-अलग variables में रखने के बजाय हम उन्हें एक object में रख सकते हैं:
ऊपर student एक object है। इसमें name, age, course और city properties हैं। इस तरह related information को एक ही object के अंदर organize किया जा सकता है।
Object का Basic Syntax
let objectName = {
property1: value1,
property2: value2,
property3: value3
};
Object बनाने के लिए curly braces {} का use किया जाता है। प्रत्येक property के बाद colon : लगाया जाता है और अलग-अलग properties को comma , से separate किया जाता है।
Object Properties
Object के अंदर stored data को properties कहा जाता है। Property का नाम key होता है और उसके साथ associated data उसकी value होती है।
एक object में अलग-अलग data types की values भी रखी जा सकती हैं, जैसे string, number, boolean, array और दूसरा object।
इस object में अलग-अलग types की values रखी गई हैं। name में string, age में number, isStudent में boolean और skills में array है।
Accessing Object Properties
Object की properties को access करने के दो common तरीके हैं:
- Dot Notation
- Bracket Notation
Dot Notation
Dot notation में object के नाम के बाद dot . लगाकर property का नाम लिखा जाता है।
यहाँ person.name से name property की value access की गई है। इसी तरह दूसरी properties को भी access किया जा सकता है।
Bracket Notation
Bracket notation में property का नाम square brackets [] के अंदर string के रूप में लिखा जाता है।
Bracket notation का एक important फायदा यह है कि property का नाम किसी variable से dynamically भी दिया जा सकता है।
Adding and Updating Object Properties
JavaScript objects dynamic होते हैं। इसका मतलब है कि object बनने के बाद भी हम उसमें नई properties add कर सकते हैं और existing properties की value update कर सकते हैं।
New Property Add करना
ऊपर object बनने के बाद उसमें city और course properties add की गई हैं।
Existing Property Update करना
अगर किसी existing property को नई value assign की जाती है, तो उसकी पुरानी value replace हो जाती है।
Deleting Object Properties
Object से किसी property को remove करने के लिए delete operator का use किया जाता है।
delete person.age execute होने के बाद age property object से remove हो जाएगी।
Object Methods
Object के अंदर केवल data values ही नहीं, बल्कि functions भी store किए जा सकते हैं। जब कोई function object की property के रूप में मौजूद होता है, तो उसे commonly method कहा जाता है।
ऊपर greet object का method है। इसे user.greet() के रूप में call किया गया है।
this Keyword
Object के method के अंदर this keyword का use current object को refer करने के लिए किया जा सकता है। ऊपर के example में this.name current user object की name property को refer करता है।
Creating Object Using new Keyword
Object literal {} सबसे common तरीका है, लेकिन JavaScript में new Object() का use करके भी object बनाया जा सकता है।
इस example में पहले एक empty object बनाया गया और बाद में उसमें properties add की गईं। हालांकि सामान्य object creation के लिए object literal {} syntax अधिक common और concise है।
Nested Objects
एक object के अंदर दूसरा object भी रखा जा सकता है। इसे nested object कहा जाता है। यह complex data को organize करने के लिए useful होता है।
यहाँ address स्वयं एक object है। इसलिए उसके अंदर की property को student.address.city जैसे access किया गया है।
Object Destructuring
जब हमें object की कुछ properties को अलग-अलग variables में आसानी से निकालना हो, तब object destructuring का use किया जा सकता है। यह modern JavaScript में commonly used feature है।
इस example में object की name, age और city properties को directly अलग variables में निकाल लिया गया है।
Object.keys(), Object.values() और Object.entries()
JavaScript में कुछ built-in methods हैं जिनकी मदद से object की keys और values को आसानी से प्राप्त किया जा सकता है।
Object.keys()
Object.keys() object की सभी enumerable property names की एक array return करता है।
Object.values()
Object.values() object की property values की एक array return करता है।
Object.entries()
Object.entries() key और value के pairs की array return करता है।
Objects और Arrays
JavaScript में objects और arrays को साथ में भी use किया जाता है। उदाहरण के लिए किसी website पर multiple students का data रखने के लिए objects की array बनाई जा सकती है।
यह structure real applications में बहुत common है। API से मिलने वाला data भी अक्सर objects की array के रूप में मिलता है।
JavaScript Objects के Real-World Uses
JavaScript objects का use web development के लगभग हर important area में होता है। किसी entity की related information को organized तरीके से store करने के लिए objects बहुत useful हैं।
- User Data: name, email, age और profile information store करने के लिए।
- Product Data: product name, price, category और stock जैसी information के लिए।
- Student Data: student name, course, marks और other details के लिए।
- API Data: APIs से मिलने वाले structured data को handle करने के लिए।
- DOM: JavaScript में browser के कई built-in objects के साथ काम किया जाता है।
- Configuration: application settings और configuration values रखने के लिए।
- State Management: applications में current data या state represent करने के लिए।
Object और Array में Difference
| Object | Array |
|---|---|
| Data को named properties के रूप में store करता है। | Data को ordered elements के रूप में store करता है। |
| Property को key से access किया जाता है। | Elements को index से access किया जाता है। |
Example: person.name |
Example: students[0] |
JavaScript Objects के Important Points
- Object related data को एक structure में organize करने में मदद करता है।
- Object की information key-value pairs में store होती है।
- Object को सबसे commonly
{}object literal syntax से बनाया जाता है। - Properties को dot notation और bracket notation से access किया जा सकता है।
- Object बनने के बाद नई properties add की जा सकती हैं।
- Existing properties की values update की जा सकती हैं।
deleteoperator से properties remove की जा सकती हैं।- Object के अंदर function को method कहा जाता है।
thiskeyword object के context में current object को refer कर सकता है।- एक object के अंदर दूसरा object भी रखा जा सकता है।
- Objects की array बनाकर multiple records को represent किया जा सकता है।
- Objects APIs और web applications में बहुत commonly use होते हैं।
Conclusion
JavaScript Objects programming का एक बहुत important concept है। Objects की मदद से हम किसी एक entity से related multiple information को एक organized structure में रख सकते हैं। Properties, methods, nested objects, destructuring और built-in object methods जैसे concepts JavaScript applications में बार-बार use होते हैं।
जब आप आगे DOM, APIs, JSON, events और modern JavaScript applications सीखेंगे, तब objects का use और भी ज्यादा दिखाई देगा। इसलिए JavaScript में strong programming foundation बनाने के लिए objects को अच्छी तरह समझना जरूरी है।
Request
Leave a Comment
INITIALIZING...
INITIALIZING...
Comments Total: 0 comments