OOP in JavaScript
-
Upload
manugoel2003 -
Category
Technology
-
view
4.738 -
download
1
description
Transcript of OOP in JavaScript
![Page 1: OOP in JavaScript](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/1.jpg)
OOP in JavaScript“The World's Most Misunderstood Programming Language”
Douglas Crockford
![Page 2: OOP in JavaScript](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/2.jpg)
{ }()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](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/3.jpg)
{ }()OOP Concepts
Encapsulation Inheritance Polymorphism
![Page 4: OOP in JavaScript](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/4.jpg)
{ }()Encapsulation
Definition Data + Operation Single logically organized unit Data visibility
![Page 5: OOP in JavaScript](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/5.jpg)
{ }()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](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/6.jpg)
{ }()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](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/7.jpg)
{ }()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](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/8.jpg)
{ }()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](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/9.jpg)
{ }()Inheritance
See Listing 1 See Listing 2
![Page 10: OOP in JavaScript](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/10.jpg)
{ }()Polymorphism
See Listing 1
![Page 11: OOP in JavaScript](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/11.jpg)
{ }()What more?
JSON Closures
![Page 12: OOP in JavaScript](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/12.jpg)
{ }()JSON
Object – { } Array – [ ] Separator – , :
Var students = [
{name: “abc”, age: 20},
{name: “xyz”, age: 21}
];
![Page 13: OOP in JavaScript](https://reader036.fdocuments.net/reader036/viewer/2022082807/55491e52b4c90547498b7892/html5/thumbnails/13.jpg)
{ }()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