JavaScript Object Prototype In Hindi

📔 : Java Script 🔗

पिछले topic में आपने Object constructor के बारे में पढ़ा और सीखा कि कैसे properties / methods को constructor में add कर सकते हैं। लेकिन वहां पर हम constructor function के through property / method को add करते थे जिसकी वजह से वह property / method किसी object से accessible नहीं था सिर्फ constructor function के through ही access कर सकते थे।


because constructor function के द्वारा add की गयी property / methods Object scope में नहीं होते थे , जिससे वो Object के through accessible ही नहीं होते थे। और scope में न होने की वजह से add किये गए methods में this keyword के through किसी property को access भी नहीं किया जा सकता था।

For Example :

File : js_object_constructor.html

CopyFullscreenClose Fullscreen
<!DOCTYPE html>
<html>
  <meta charset="utf-8">
  <body>
    <script>
      function Person(first, last) {
        this.first_name = first;
        this.last_name = last;
      }

      /*add method*/
      Person.full_name = function(){
        return "return function";
      }
      
      /*we can call it like this*/
      Person.full_name();

      /*but we can't access it like this*/
      let my_friend = new Person("Mohit", "Rajput");
      my_friend.full_name();
    </script>
  </body>
</html>

और ठीक इसी तरह जब किसी Object variable के through किसी property या method को add करते थे तो वो property / method सिर्फ उसी object variable के लिए add होता था , किसी दूसरे Object variable के लिए नहीं।

/*add a method*/
my_friend1.full_name = function(){
  return this.first_name+' '+this.last_name;
}

/*we can accees it only with my_friend1*/
my_friend1.full_name();

/*we can't access it with my_friend2 */
let my_friend2 = new Person("Girish", "Shekhawat");
my_friend2.full_name();

इन्ही सब गलतियों से बचने के लिए JavaScript prototype use करते हैं ताकि एक बार किसी property / method को add करने के बाद बो सभी Object variables के साथ accessible हो।


JavaScript में सभी Objects properties / methods को prototype से ही inherit करते हैं -

  1. Date objects inherit from Date.prototype
  2. Array objects inherit from Array.prototype

prototype को आप किसी Object को console में print कराकर easily देख सकते हैं।

JavaScript Object Prototype Example

File : js_object_prototype.html

CopyFullscreenClose FullscreenRun
<!DOCTYPE html>
<html>
  <meta charset="utf-8">
  <body>
    <script>
      function Person(first, last) {
        this.first_name = first;
        this.last_name = last;
      }

      /*add a method*/
      Person.prototype.full_name = function(){
        return this.first_name+' '+this.last_name;
      }

      let my_friend1 = new Person("Mohit", "Rajput");

      /*access method*/
      document.writeln(my_friend1.full_name());

      /*initialize another Object*/
      let my_friend2 = new Person("Girish", "Shekhawat");
      document.writeln(my_friend2.full_name());
    </script>
  </body>
</html>
Output
Mohit Rajput 
Girish Shekhawat

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