JavaScript Object Constructor In Hindi

📔 : Java Script 🔗

जैसा कि आप पढ़ चुके हैं कि JavaScript में primitives को छोड़कर लबभग सब कुछ Object है , अगर आप इसका type check करें तो पायंगे कि functions भी Object होते हैं। new keyword के through आप किसी भी function का Object बना सकते हैं।

File : js_object_fun.html

CopyFullscreenClose FullscreenRun
<!DOCTYPE html>
<html>
  <meta charset="utf-8">
  <body>
    <script>
      function myfun(){
        /*code*/
      }
      /*open console and see */
      console.log(new myfun());
    </script>
  </body>
</html>

इन्ही functions को आप as a constructor use कर सकते हो।

JavaScript Object Constructor Example

File : js_object_constructor.html

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

      let my_friend = new Person("Mohit", "Rajput", 23, "India");

      /*you can use it any number of times*/
      let my_friend2 = new Person("Girish", "Shekhawat", 23, "India");
      document.writeln("Full Name :"+ my_friend.first_name + ' '+my_friend.last_name);
      document.writeln("Age :"+ my_friend.age);
      document.writeln("Address :"+ my_friend.address);
    </script>
  </body>
</html>
Output
Full Name :Mohit Rajput 
Age :23 
Address :India
Important

JavaScript this Keyword

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

JavaScript Adding a Property to an Object

Object initialize करने के बाद आप directly किसी भी new property/method को dot operator . के through add कर सकते हैं या existing property को update कर सकते हैं।
For Example :

let my_friend = new Person("Mohit", "Rajput", 23, "India");

/*add new property*/
my_friend.eye = "Black";

/*update property*/
my_friend.age = 25;

JavaScript Adding a Property to a Constructor

Constructor में property add करने के लिए आपको constructor function का use करना पड़ता है , object का use करके किसी property/method को constructor में add नहीं कर सकते हैं। constructor में add की गयी property/method को Object के through access नहीं कर पाएंगे , constructor function के through ही access कर पायंगे।
Example :

Person.eye = "black";
/*we can access it*/
Person.eye;

/*we can't access it like this */

let my_friend = new Person("Mohit", "Rajput", 23, "India"); my_friend.eye;

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