13
OOP in JavaScript “The World's Most Misunderstood Programming Language” Douglas Crockford

OOP in JavaScript

Embed Size (px)

DESCRIPTION

Manu - OOP in JavaScript

Citation preview

Page 1: OOP in JavaScript

OOP in JavaScript“The World's Most Misunderstood Programming Language”

Douglas Crockford

Page 2: OOP in JavaScript

{ }()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

Page 3: OOP in JavaScript

{ }()OOP Concepts

Encapsulation Inheritance Polymorphism

Page 4: OOP in JavaScript

{ }()Encapsulation

Definition Data + Operation Single logically organized unit Data visibility

Page 5: OOP in JavaScript

{ }()Inheritance

Definition Parent-child relationship between classes Child inherits Parents’ characteristics Child can redefine existing characteristics Child can add more characteristics

Page 6: OOP in JavaScript

{ }()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)

Page 7: OOP in JavaScript

{ }()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

Page 8: OOP in JavaScript

{ }()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++;}

Page 9: OOP in JavaScript

{ }()Inheritance

See Listing 1 See Listing 2

Page 10: OOP in JavaScript

{ }()Polymorphism

See Listing 1

Page 11: OOP in JavaScript

{ }()What more?

JSON Closures

Page 12: OOP in JavaScript

{ }()JSON

Object – { } Array – [ ] Separator – , :

Var students = [

{name: “abc”, age: 20},

{name: “xyz”, age: 21}

];

Page 13: OOP in JavaScript

{ }()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