JavaScript Static Properties and Methods In Hindi : JavaScript Class

📔 : Java Script 🔗

पिछले Topic में आपने समझा कि JavaScript में Class और Prototypes क्या होते हैं और इन्हे कैसे define करते हैं। हालाँकि किसी भी Class में दो तरह के members (proeprties / methods ) हो सकते हैं , static & non ntatic .

Non Static proeprties / methods को हम बिना किसी keyword के define करते हैं और class object बनाकर access कर सकते हैं। जिसे आपने पिछले topics में देखा। एक छोटा सा example नीचे दिए गया है।

class User { name = "Shyam Lal"; hello() { return `Hello! ${this.name}`; } } // create object to access non-static property and method. const user = new User(); console.log(user.name); console.log(user.hello());

Output :

Shyam Lal
Hello! Shyam Lal

Example में आप देख सकते हैं , class के property और method को access करने के लिए हमें class का object बनाना पड़ा है , इस type के members को non-static members कहते हैं।

इस Topic में आप पढ़ेंगे कि Class में Static Members क्या होता है , कैसे इन्हे define करते हैं और किस - किस तरह से इन्हे Access कर सकते हैं।

JS Static Properties And Methods

Well , Class में static members वो members होते हैं जिन्हे define करने के लिए static keyword का use किया जाता है , और इन members को बिना instance create किये directly class name के साथ access किया जाता है।

JS static member example

class MyClass { // Static property static staticProperty = "I am a static property"; // Static method static staticMethod() { return "I am a static method"; } } // Accessing the static property and method without creating an instance. console.log(MyClass.staticProperty); console.log(MyClass.staticMethod());

Output :

I am a static property
I am a static method

Example में आप देख सकते हैं , class के static property और method को access करने के लिए हमें class का instance / object नहीं बनाना पड़ा है , directly Class Name के साथ access किया है ।

हालाँकि अगर आप object instance के साथ static members को access करने का try करेंगे तो property में undefined मिलेगा और method के लिए error generate होगी।

let obj = new MyClass();

console.log(obj.staticProperty); undefined
obj.staticMethod(); Uncaught TypeError: obj.staticMethod is not a function

You can not access `this` inside static method

हालाँकि , आप static property और method को class instance के साथ access नहीं कर सकते हैं , जिसका मतलब है कि static methods के अंदर current object this available नहीं होता है , जिससे आप current class की non-static members को access नहीं कर पाएंगे।

For example :

class User { // non-static property. user_name = "Shyam Lal"; // static method. static sayHi() { console.log(`Hi ${this.user_name}`); } } User.sayHi(); // Hi undefined .
Pass class instance to access non static members inside static method

अभी आपने देखा कि class की non-static members को static methods में this का use करके तो access नहीं कर सकते हैं , लेकिन हाँ static method में class instance pass करके non-static members को access किया जा सकता है।

class User { user_name = "Shyam Lal"; getAddress() { return "UP, India"; } // now define instance variable in static method. static sayHi(obj) { console.log(`Hi ${obj.user_name} from ${obj.getAddress()}.`); } } const user = new User(); // now pass Class instance in static method User.sayHi(user); // Hi Shyam Lal from UP, India.

Use class name to access static members inside static method

किसी class के static members को static methods में access करने के लिए directly class name का use किया जा सकता है।

For example :

class User { // static property & method. static user_name = "Shyam Lal"; static getAddress() { return "UP, India"; } static sayHi(obj) { // use class name `User` to access static property & method. console.log(`Hi ${User.user_name} from ${User.getAddress()}.`); } } User.sayHi(); // Hi Shyam Lal from UP, India.

I Hope guys , आपको JavaScript में किसी class के अंदर static property और method का concept अच्छे से समझ आया होगा।

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