JavaScript Classes In Hindi | JavaScript OOP Concept In Hindi

📔 : Java Script 🔗

हम जानते हैं कि JavaScript में functions को as a constructor भी use कर सकते हैं मतलब functions का object बनाकर , उनमे properties define कर सकते हैं।

लेकिन modern JavaScript में इससे भी अच्छा और advance feature है जिसे class construct . जिसे object-oriented programming के लिए काफी useful है।

***

OOP (Object Oriented Programing) Classes और Objects पर based एक Programming Paradigm / Approach है। Simple भाषा में कहें तो OOP (Object Oriented Programing) Real World Entity/Object को Programming में represent करने का method / way है।

हालाँकि JavaScript prototype-based Object oriented programming language है , Java / C++ / PHP की तरह Class Based नहीं। लेकिन फिर भी कुछ तरीके हैं जिनके through आप OOP concept को implement कर सकते हैं। अगर आप किसी भी Object को console में print कराकर देखेंगे तो उसमे आपको एक __proto__ name की property मिलेगी।

Real World Entity को ही Programing में Class / Object द्वारा represent किया जाता है। Entity की कुछ Properties या Behavior होता है जिसे Programing में Class variables & methods से represent किया जाता है।

हालाँकि JavaScript में आप दूसरी तरह से भी Object बना सकते हैं कि आप शायद पिछले topics में पढ़ चुके होंगे।

Read More About JavaScript Object

What is class

Class किसी Object के लिए एक Blueprint होता है।
For Example -
Car - एक blueprint है ,
Car के different - different brand (Maruti, Ford , Audi etc. ) Objects हैं।

JS Class Syntax

usually , class keyword का use करके JavaScript में classes define की जाती हैं।

class YourClass { // class methods constructor() { } method1() { } method2() { } method3() { } }

इसके बाद new keyword के though create object से आप class की किसी भी property या method को access कर सकते हैं।

const obj = new YourClass ();

Note* constructor() method , automatically execute हो जाता है जब भी आप किसी class का object बनाते हो इसे manually call करने की जरूरत नहीं।

JS Class Example

class User { constructor(name) { this.name = name; } // define method sayHi() { console.log(`Hi ${this.name}`); } } // create object let user = new User("Babu Rao"); user.sayHi();

Output :

Hi Babu Rao

JS Constructor Method

Class में The constructor method एक special method है -

  • यह object properties को initialize करता है, जैसा कि अभी अपने example में देखा।

  • जब भी new object create होता है तो यह automatically execute हो जाता है , इसे manually call करने की जरूरत नहीं।

  • इसे हमेशा class के अंदर constructor() नाम ही दिया जाता है।

JS Constructor Example

class MyClass() { constructor() { console.log("Object created"); } } // now create object. new MyClass();

output

Object created

Important *

JavaScript में class भी एक function ही है -

console.log(typeof User); // function

class के सभी methods prototype में होते हैं जिसका मतलब है कि class object न बनाकर directly className.prototype के साथ method को call कर सकते हैं।

User.prototype.sayHi // the code of the sayHi method

और className.prototype के साथ constructor और class name से आप comparison भी करा सकते हैं।

console.log(User === User.prototype.constructor); // true

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