If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Objects real world entity होते हैं जैसे - Car , Bike , Human etc , बाकी languages की तरह ही JavaScript में भी आप Object को define कर सकते हैं।
जैसे Real World Entity की कुछ Properties या Behavior होता है जिसे Programing में variables & methods से represent किया जाता है।
For Example -
Object | Properties | Methods |
---|---|---|
Bike |
bike.name = TVS Apache bike.weight = 150kg bike.color = White bike.max_speed = 110 to 160 km/h |
bike.start() bike.drive() bike.brake() bike.stop() |
JavaScript में Object बहुत ही important हैं , क्योंकि कुछ primitives को छोड़कर सभी values Object ही हैं।
primitive value वो values होती हैं , जिकी कोई property & methods नहीं होते हैं। primitive data type वो type होते हैं जिनकी value primitive होती है।
JavaScript में 5 primitivesहैं -
❕ Important
primitive values immutable होती हैं means इन्हे change नहीं किया जा सकता है -
for example - let x = 10; यहाँ पर आप x की values तो change कर सकते हैं लेकिन 10 जो की primitive value है यह 10 ही रहेगा इसे आप change नहीं कर सकते हैं।
हालाँकि normal variable जो कि primitive value contain करता है, और Objects दोनों ही variables ही हैं लेकिन normal variables किसी single value को ही contain कर सकते हैं।
For Example -
let x = 10;
जबकि Object में एक से ज्यादा values हो सकती हैं। Object में value kay : value pair में define किया जाता है। जिसमे key और value को colon : से separate किया जाता है , और एक से ज्यादा values को comma , से separate किया जाता है।
? JavaScript में Objects mutable होते हैं , means need के according हम कभी भी इसमें property add कर सकते हैं।
JavaScript में Object को दो तरह से define कर सकते हैं -
अब अगर ऊपर दिखाए गए Example को अगर javaScript में Object के through represent करना हो तो , कुछ इस तरह define करेंगे -
File : file.html
<!DOCTYPE html>
<html>
<body>
<script>
/*define Object using curly braces*/
let bike = {
name : "TVS Apache",
weight : "150kg",
color : "White",
max_speed : "110 to 160 km/h",
start : function(){
return "Bike start";
}
};
/*define Object using curly braces*/
let bike = new Object();
bike.name = "TVS Apache";
bike.weight = "150kg";
bike.color = "White";
bike.max_speed = "110 to 160 km/h";
bike.start = function(){
return "Bike start";
};
</script>
</body>
</html>
दोनों में से कोई तरीका आप Object create करने के लिए use कर सकते हैं।
? कभी भी predefined Objects जैसे String , Math , Array , Number , Boolean को as a Object define न करें।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook