JavaScriptలో Objects
JavaScript objects సంబంధిత డేటాను key-value pairs రూపంలో నిల్వ చేస్తాయి. ప్రతి key దాని విలువకు (value) ఒక ప్రత్యేక గుర్తింపుగా (unique identifier) పనిచేస్తుంది.
ఒక objectలో ఉండే విలువలు:
- Primitives
- ఇతర objects
- Functions (వీటిని methods అంటారు)
Objects అనేవి mutable (మార్చదగినవి). మీరు ఎప్పుడైనా propertiesని జోడించవచ్చు, మార్చవచ్చు లేదా తొలగించవచ్చు. ఇది డేటా మరియు ప్రవర్తనను (behavior) కలిపి ఉంచడానికి సహాయపడుతుంది.
Objectsని సృష్టించడానికి రెండు మార్గాలు:
Object Literals Propertiesని నేరుగా నిర్వచించడానికి curly braces ఉపయోగించండి. ఉదాహరణ: const user = { name: "Vidhya", age: 23, job: "Developer" };
New Object Constructor Object() constructorని ఉపయోగించండి. ఉదాహరణ: const user = new Object(); user.name = "Alex"; user.age = 4;
Objectsతో ఎలా పనిచేయాలి:
- డేటాను యాక్సెస్ చేయడం: dot notation (user.name) లేదా bracket notation (user["name"]) ఉపయోగించండి.
- డేటాను మార్చడం: ఒక property విలువను మళ్ళీ కేటాయించండి (user.age = 25).
- డేటాను జోడించడం: కొత్త keyని సెట్ చేయడానికి dot లేదా bracket notation ఉపయోగించండి.
- డేటాను తొలగించడం: delete operatorని ఉపయోగించండి.
- Propertiesని తనిఖీ చేయడం: "in" operator లేదా hasOwnProperty() methodని ఉపయోగించండి.
- Looping: అన్ని propertiesని పరిశీలించడానికి for...in loopని ఉపయోగించండి.
- Merging: Object.assign() లేదా spread syntax (...) ఉపయోగించండి.
- Propertiesని లెక్కించడం: Object.keys() ఉపయోగించండి.
మీ references స్థిరంగా ఉండటానికి ఎల్లప్పుడూ మీ objectsని constతో డిక్లేర్ చేయండి.
Source: https://dev.to/vidhya_murali_5aabe7784bd/objects-in-javascript-5600