|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis article, 1st Part in the series of 3-article, starts discussion on OOPS implementation in JavaScript. Note: This article is an adaption (may be a mirror) of the article originally posted at Edujini™ Eduzine™ here. All the three articles of the series:
FundamentalsObect Oriented Programming - as defined by Grady Booch - stands on three pillars:
Here, we explore how all three - Yes! All Three Pillars of OOPS - are implemented in JavaScript. Getting StartedTo get started, it is assumed that you are already familiar with basic JavaScript. If not you can look on the web for some tutorials on JavaScript 101 tutorials or the like. To be specific, the following fundamentals are assume:
4-Sutras while working with JavaScriptWhile working with JavaScript, always keep in mind the following, as Gaurav Vaish (yes, that's me) calls them, "4-Sutras":
"In JavaScript, whenever you run into any problem related to implementation or structuring of the code, these four sutras will help you out", I say. EncapsulationEncapsulation - in simplest terms - can be defined as putting the related items together. Structures in C is a way to implement encapsulation. Classes in C++, Java, C# etc is another way to implement encapsulation. Let us see how encapsulation is implemented in JavaScript. Welcome, Functions!Gaurav's 4th Sutra comes into action here - "It's all about functions".
Let us define a data-type
Let us implement the /** * (C) 2008, Gaurav Vaish, Edujini Labs Pvt Ltd * http://www.edujini-labs.com */ function UserProfile(username, password) { this.username = username; this.password = password; this.authenticate = function() { if(this.username == 'gvaish' && this.password == 'edujini') { return true; } return false; } }
Notice the use of Test Case
Ok... so, how do we use this new data-type that we just defined? Let's create a HTML file that will serve as the playground for our case-studies around the 1 <html> 2 <head> 3 <title>OOPS in JavaScript- Encapsulation</code> 4 <script language="'javascript'" type='text/javascript' src='UserProfile.js'></script> 5 <script language="'javascript'" type='text/javascript'> 6 /** 7 * (C) 2008, Gaurav Vaish, Edujini Labs Pvt Ltd 8 * http://www.mastergaurav.com 9 * http://eduzine.edujini-labs.com 10 */ 11 function validateUser() 12 { 13 var uname = document.getElementById('u').value; 14 var pwd = document.getElementById('p').value; 15 16 var e = document.getElementById('result'); 17 18 var u = new UserProfile(uname, pwd); 19 var result = u.authenticate(); 20 e.innerHTML = 'Result: ' + result; 21 } 22 </script> 23 </head> 24 <body> 25 26 Username: <input type='text' name='u' id='u' /> 27 <br/> 28 Password: <input type='password' name='p' id='p' /> 29 <br/> 30 31 <button onclick='validateUser(); return false;'>Login</button> 32 33 <div id='result'></div> 34 35 </body> 36 </html> AnalysisLet's just analyze the code that we just wrote. We have designed a simple form comprising of inputs for username and password, and a button to trigger validation.
The function
At line 18,
At line 19, we invoke the method So, go ahead and try out with different combinations of username and password, and checkout the results! SummaryIn this article we learnt how encapsulation is implemented in JavaScript. To summarize, we explored the following:
Moving forward...The next step is look at the next articles on Inheritance and Polymorphism in JavaScript.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||