Upload
manugoel2003
View
4.737
Download
1
Embed Size (px)
DESCRIPTION
Manu - OOP in JavaScript
Citation preview
OOP in JavaScript“The World's Most Misunderstood Programming Language”
Douglas Crockford
{ }()Some Myths
JavaScript is related to Java JavaScript was developed by Sun
Microsystems JavaScript is not Object Oriented JavaScript does not support data hiding, i.e.
everything is public
{ }()OOP Concepts
Encapsulation Inheritance Polymorphism
{ }()Encapsulation
Definition Data + Operation Single logically organized unit Data visibility
{ }()Inheritance
Definition Parent-child relationship between classes Child inherits Parents’ characteristics Child can redefine existing characteristics Child can add more characteristics
{ }()Polymorphism
Definition Multiple functions Share a common name Context specific invocation
The characteristic of being able to assign a different meaning or usage to something in different contexts (http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212803,00.html)
{ }()Encapsulation
Private variables - Can only be accessed by private functions and privileged methods
Private functions - Can only be called by privileged methods
Privileged methods - May invoked by code external to the object
Public properties - May be read/written from outside the object
Public methods - Shared methods
Prototype properties - Shared properties
{ }()Encapsulation
function Student(name){
var age = 0;var checkAge = function(){ return (age > 0) ? age : false; };this.getAge = function(){
return (checkAge()) ? age : “Age not set”; }this.setAge = function(_age){ age = _age; }
this.name = name;this.constructor.prototype.updateTotal();
}Student.prototype.total = 0;Student.prototype.updateTotal = function(){
this.constructor.prototype.total++;}
{ }()Inheritance
See Listing 1 See Listing 2
{ }()Polymorphism
See Listing 1
{ }()What more?
JSON Closures
{ }()JSON
Object – { } Array – [ ] Separator – , :
Var students = [
{name: “abc”, age: 20},
{name: “xyz”, age: 21}
];
{ }()References
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference
http://mckoss.com/jscript/object.htm http://www.sitepoint.com/article/oriented-programming-1 http://www.codeproject.com/aspnet/JsOOP1.asp http://phrogz.net/JS/Classes/OOPinJS.html http://javascript.crockford.com/javascript.html http://kevlindev.com/tutorials/javascript/inheritance/index.htm http://www.dustindiaz.com/javascript-private-public-privileged/ http://developer.yahoo.com/yui/theater/ http://www.dustindiaz.com/json-for-the-masses/ http://www.json.org/ Google + Wikipedia = Many Many more references