OOPS Concepts in Javascript
javascript के ECMAScript6 version में OOP concepts को introduce किया गया है |
OOP क्या है?
ये कोई technology नहीं है, बल्कि ये program लिखने का एक तरीका है | OOP को एक coding methodology/style/pattern है |
OOP के क्या फायदे हैं?
- OOP हमे बड़े program को छोटे छोटे section में बांटने में मदद करता है | जिसके जरिए code को manage करना आसान हो जाता है |
- related code को कई module में बांटने की वजह से code reusable हो पाता है |
- code को आसानी से debug किया जा सकता है और उसमें आ रहे error को ठीक किया जा सकता है |
- OOP concept के जरिये data को secure किया जा सकता है |
- javascript में ज्यादातर OOP concepts framework में इस्तेमाल होता है |
कैसे जाने की कोई programming language OOP concepts को follow कर रहा है या नहीं ?
OOPS के कुछ features हैं जब कोई programming language वो features को follow करता है तब वो OOPs programming language कहलाता है |
OOPS के features है :
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
javascript completely OOPS language नहीं है | ये partially OOPS concepts को follow करता है क्यूंकि javascript polymorphism feature को support नहीं करता है |
अन्य programming languages जो OOP concepts को follow करतें हैं वो हैं:
- java
- C++
- .net
- python
Class
Class एक blueprint है या फिर इसे हम object का skeleton कह सकतें हैं |
जब कोई घर बनाना होता है तब पहले उसका नक्षा निकालते हैं, उसके बाद उस नक्षे के हिसाब से घर बनातें हैं | उसी तरह एक class, object के सारे properties और functionalities को represent करता है | और जब उस properties और functionalities को access करना होता है तब हम object बनातें हैं |
Syntax
class ClassName{ constructor(){.....} }
Object
object एक real time entity है | हर object के कुछ features और functionalities होतं हैं |
उदाहरण
class Employee{ constructor(name, department){ this.employeeName = name; this.employeeDepartment = department } } var obj1 = new Employee("Aman", "IT");
उदाहरण में देखिए हमने Employee नाम का class बनाया है जिसमें दो properties define की है | class को directly consume नहीं कर सकतें हैं | class को access करने के लिए object बनानी पड़ती है |
किसी class का object बनाने के लिए new keyword का उपयोग किया जाता है |
new keyword के जरिये obj1 नाम के object हमने बनाया है और Employee class को access किया है |
constructor()
constructor javascript में एक pre-defined method है | जब हम class का कोई object create करतें हैं, तब ये method automatically call होता है |
constructor method, class में define properties को value assign करने में मदद करता है |
Features of OOP
1) Encapsulation
data और methods को एक group में bind करने के process को encapsulation कहतें हैं | Encapsulation data और इससे जुड़े methods को security देता है |
class बनाना एक encapsulation का उदाहरण है |
2) Abstraction
Abstraction हमे data और method में restriction लगाने में मदद करता है की कोनसा properties class के बाहार access होगा है और कोनसा सिर्फ class के अंदर ही उपयोग होगा |
program लिखते समय हमारी कुछ ऐसी जरुरत होती है की class की कुछ properties और methods की access हम object के जरिये नहीं देना चाहते हैं और बाकि के functionalities को बाहार access देना चाहतें हैं | ऐसे में abstraction feature काम में आता है |
3) Inheritance
एक class की properties और methods को जब हम दुसरे class में access करतें हैं, उस feature को inheritance कहतें हैं | inheritance के जरिए एक class खुदकी properties और methods को own करता ही है साथ ही साथ दुसरे की properties और methods को भी access कर सकता है |
inheritance feature के जरिए classes में parent-child relationship बना सकतें हैं |
उदाहरण
<script> class College extends University{ collegeName = "JNTU"; } class Student extends College{ studentName = "Rakesh"; } var obj = new Student(); console.log(obj) </script>
Ploymorphism
same method या constructor जब अलग अलग parameters के जरिये बनाते हैं, उस feature को polymorphism कहा जाता है |
अन्य programming languanges में polymorphism को method overloading या फिर method overriding के जरिए हासील किया जा सकता है |
पर javascript method overloading और method overriding को support नहीं करता है |