JavaScript Object Methods In Hindi

📔 : Java Script 🔗

जैसा कि आपने पिछले chapter में पढ़ा कि real world Object के behavior को programming में methods से represent किया जाता है। basically methods Object द्वारा perform किये जाने वाले actions होते हैं।


JavaScript Object में आप easily किसी action को as a method represent कर सकते हैं , और need के according methods को Object में add / remove भी कर सकते हैं।


Object methods भी एक तरह से properties ही होते हैं जो function definition को contain करते हैं।

JavaScript Object Method Example

File : js_object_method.html

CopyFullscreenClose FullscreenRun
<!DOCTYPE html>
<html>
  <body>
    <script>
      var person = {
        first_name: "Rahul",
        last_name : "Kumar",
        full_name : function() {
          return this.first_name + " " + this.last_name;
        }
      };
      document.write("Full Name : " + person.full_name());
    </script>
  </body>
</html>
Output
Full Name : Rahul Kumar 
Important

JavaScript this Keyword

this keyword basically , current object को represent करता है , example में function के अंदर this.first_name का मतलब है कि current Object (person) की first_name property को access किया जा रहा है।


Object methods भी functions ही होते हैं जो as a property define किये जाते है , इसका मतलब अगर आप Object method को property की तरह without parenthesis ( ) के access करोगे तो function definition ही मिलेगी।
For Example :

document.write(person.full_name);

Output
function() { return this.first_name + " " + this.last_name; }

? Object में जब methods और properties हो तो , कभी भी Object को for .. in loop से iterate न करें जैसे properties के लिए किया था , because method call करने के लिए हमें parenthesis ( ) use करना पड़ता है। जबकि properties को हम without parenthesis ( ) भी access कर सकते हैं।

JavaScript Adding a Method to an Object

जैसा कि आपने पिछले topics में पढ़ा कि , Objects mutable होते हैं , means need के according हम कभी भी इसमें property / methods add / remove कर सकते हैं।


जिस तरह से Object में new property को add करते हैं उसी तरह methods को add किया जाता है बस property की value assign करने की जगह function definition assign की जाती है।
For Example :

person.get_age = function(){
return 23;
}

That's it. हो गया नया method add .

JavaScript Deleting Method From Object

जैसा की आपने ऊपर पढ़ा कि , methods भी एक तरह से Object properties ही होते हैं जो function definition को contain करते हैं। इसलिए method को property की तरह ही delete किया जाता है।
For Example :

delete person.get_age;

? method को Object से delete करते समय parenthesis ( ) न लगाएं , नहीं तो method delete नहीं होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

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

b2eprogrammers