SlideShare a Scribd company logo
Click to add Text 
Javascript 
Presented by Bunlong VAN 
http://geekhmer.github.io 
Object Oriented
The Disclaimer
Object Oriented Basic 
•Object (instance): representation of a “thing” (someone or 
something) 
•Class: ablueprint, or recipe for an Object 
•Encapsulation: illustrates the fact that an Object contains 
(encapsulates): 
• Data (stored in properties) 
• The means to do something with data (using 
methods) 
•Inheritance
Object Creation 
•Two simple ways 
var obj = new Object(); 
obj.name = “Foo”; 
obj.say = function() { 
return “Hello”; 
} 
var obj = { 
name: “Foo”, 
say: function() { 
return “Hello”; 
} 
}
Method 
•When a property is a function we can call it a method 
var object = { 
method: function() { 
alert(“Here is method”); 
} 
}
Prototype 
•JavaScript functions work as constructors, methods and 
modules 
•It has Prototypal Inheritance, which offers great expressive 
powers 
•All Objects are directly or indirectly link to Object.prototype 
•Each Object is linked to its parent via a magic link 
•When a member is accessed the compiler looks at the 
Object and then looks up to its parent via the magic link 
•It keeps going all the way up to Object.prototype 
•Go to console of firebug and type Object.prototype
Prototype (Con.) 
•Looking for my_object.foo, found it in the Object itself 
•Looking for my_object.baz looks in the object and if it 
doesn't find it there it goes to my_object_parent which is 
my_object.prototype 
•Looking for my_object.some_random_member can't find it in the 
object, look at my_object_parent, can't find it 
•There either, goes to Object can't find it there and is set to 
undefined
Prototype (Con.) 
var Person = function() { 
this.name = “Mr. Bunlong”; 
this.callMe = function() { 
alert(“I am ” + this.name); 
} 
} 
var personObj = new Person(); 
personObject.callMe(); 
personObject.constructor(); 
personObject.myFoo(); // will return underfined
Object Augm entation 
•New members can be added to any object 
Person.prototype.callMeAgain = function() { 
alert(“I said my name is ” + this.name); 
}; 
personObj.callMeAgain();
Prototype Usage 
•CallMeAgain() is a property of the prototype object 
•But it behaves as if it's a prototype of the dude object
Own Properties Vs Prototype's 
•personObj.hasOwnProperty(“callMe”); // will return true 
•personObj.hasOwnProperty(“callMeAgain”); // will return false
isPrototypeOf 
•Person.prototype.isPrototypeOf(personObj); // will return true
Inheritance 
•OOP ensures code reuse 
•Two forms of OOP 
• Classical with Mixin 
• Prototypal
Prototypal Inheritance 
var Boy = function() {}; 
Boy.prototype = new Person(); 
var boyObj = new Boy(); 
boyObj.callMe();
Inheritance through Mixin 
var Person = function(name) { 
this.greet = function() { 
alert(“Hello, I'm ” + name); 
} 
}; 
var Employee = function(name) { 
person.apply(this, [name]); 
this.getTeam = function() {return “UI Library”;} 
} 
// instantiate object of Employee 
var employee = new Employee(“Bunlong”); 
employee.greet(); 
var team = employee.getTeam(); 
alert(“Team: ” + team);
Singleton 
•There is no need to produce a class like constructor for an 
object that will have exactly one instance 
•This is typical of JavaScript applications 
•Use an object literal to get started instead
Module Pattern 
•The methods of a singleton can enjoy access to shared 
private datum and private methods 
•Variables defined in a module are only visible in a module 
•Variables defined in a function are only visible to the 
function 
•Function can be used as module containers
Module Pattern (Con.) 
var myModule = function() { 
var privateVariable, 
privateFunction = function() { 
// coding here 
}; 
return { 
FirstMethod: function() { 
// can access privateVariable 
// can access privateFunction 
} 
} 
}(); 
myModule.firstMethod();
Javascript Object Oriented Programming

More Related Content

PDF
Prototype
Aditya Gaur
 
KEY
2012 oct-12 - java script inheritance
pedro.carvalho
 
PPTX
Javascript Prototype Visualized
军 沈
 
PDF
Object Oriented Programming in JavaScript
zand3rs
 
PPTX
Js: master prototypes
Barak Drechsler
 
PDF
JavaScript Patterns
Stoyan Stefanov
 
PDF
JavaScript Inheritance
Jussi Pohjolainen
 
PDF
Javascript Design Patterns
Subramanyan Murali
 
Prototype
Aditya Gaur
 
2012 oct-12 - java script inheritance
pedro.carvalho
 
Javascript Prototype Visualized
军 沈
 
Object Oriented Programming in JavaScript
zand3rs
 
Js: master prototypes
Barak Drechsler
 
JavaScript Patterns
Stoyan Stefanov
 
JavaScript Inheritance
Jussi Pohjolainen
 
Javascript Design Patterns
Subramanyan Murali
 

What's hot (19)

PPT
Prototype Js
Kivanc Kanturk
 
PPT
Object Oriented JavaScript
Donald Sipe
 
PDF
Scalable JavaScript Design Patterns
Addy Osmani
 
PPT
JavaScript In Object Oriented Way
Borey Lim
 
PDF
Future-proofing Your JavaScript Apps (Compact edition)
Addy Osmani
 
PDF
JavaScript Objects
Hazem Hagrass
 
KEY
Week3
Will Gaybrick
 
PPTX
Jquery fundamentals
Salvatore Fazio
 
PDF
The Django Book, Chapter 16: django.contrib
Tzu-ping Chung
 
PPTX
Javascript Common Design Patterns
Pham Huy Tung
 
PPTX
Powerful Generic Patterns With Django
Eric Satterwhite
 
PPTX
Introduction to Design Patterns in Javascript
Santhosh Kumar Srinivasan
 
PPTX
Javascript for the c# developer
Salvatore Fazio
 
PDF
JS Level Up: Prototypes
Vernon Kesner
 
PPTX
Object Oriented JavaScript - II
TO THE NEW | Technology
 
PDF
[A 3]Javascript oop for xpages developers - public
Kazunori Tatsuki
 
PPTX
JavaScript Literacy
David Jacobs
 
PPTX
jQuery Data Manipulate API - A source code dissecting journey
Huiyi Yan
 
PDF
Metaprogramming + Ds Ls
ArrrrCamp
 
Prototype Js
Kivanc Kanturk
 
Object Oriented JavaScript
Donald Sipe
 
Scalable JavaScript Design Patterns
Addy Osmani
 
JavaScript In Object Oriented Way
Borey Lim
 
Future-proofing Your JavaScript Apps (Compact edition)
Addy Osmani
 
JavaScript Objects
Hazem Hagrass
 
Jquery fundamentals
Salvatore Fazio
 
The Django Book, Chapter 16: django.contrib
Tzu-ping Chung
 
Javascript Common Design Patterns
Pham Huy Tung
 
Powerful Generic Patterns With Django
Eric Satterwhite
 
Introduction to Design Patterns in Javascript
Santhosh Kumar Srinivasan
 
Javascript for the c# developer
Salvatore Fazio
 
JS Level Up: Prototypes
Vernon Kesner
 
Object Oriented JavaScript - II
TO THE NEW | Technology
 
[A 3]Javascript oop for xpages developers - public
Kazunori Tatsuki
 
JavaScript Literacy
David Jacobs
 
jQuery Data Manipulate API - A source code dissecting journey
Huiyi Yan
 
Metaprogramming + Ds Ls
ArrrrCamp
 
Ad

Similar to Javascript Object Oriented Programming (20)

KEY
Javascript tid-bits
David Atchley
 
PPTX
Framework prototype
DevMix
 
PPTX
Framework prototype
DevMix
 
PPTX
Framework prototype
DevMix
 
PPTX
Object oriented javascript
Usman Mehmood
 
PDF
JavaScript Core
François Sarradin
 
PPT
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
PPTX
JavsScript OOP
LearningTech
 
PPT
Advanced Javascript
Manikanda kumar
 
PPT
Advanced Javascript
Adieu
 
PPT
Advanced Javascript
relay12
 
PPT
Advanced JavaScript
Stoyan Stefanov
 
PPTX
Ajaxworld
deannalagason
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPTX
JavaScript OOPS Implimentation
Usman Mehmood
 
PPTX
Object Oriented Javascript part2
Usman Mehmood
 
PPTX
Object oriented javascript
Shah Jalal
 
PDF
Prototype 120102020133-phpapp02
plutoone TestTwo
 
PPTX
IWT presentation121232112122222225556+556.pptx
dgfs55437
 
PPTX
Understanding-Objects-in-Javascript.pptx
MariaTrinidadTumanga
 
Javascript tid-bits
David Atchley
 
Framework prototype
DevMix
 
Framework prototype
DevMix
 
Framework prototype
DevMix
 
Object oriented javascript
Usman Mehmood
 
JavaScript Core
François Sarradin
 
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
JavsScript OOP
LearningTech
 
Advanced Javascript
Manikanda kumar
 
Advanced Javascript
Adieu
 
Advanced Javascript
relay12
 
Advanced JavaScript
Stoyan Stefanov
 
Ajaxworld
deannalagason
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
JavaScript OOPS Implimentation
Usman Mehmood
 
Object Oriented Javascript part2
Usman Mehmood
 
Object oriented javascript
Shah Jalal
 
Prototype 120102020133-phpapp02
plutoone TestTwo
 
IWT presentation121232112122222225556+556.pptx
dgfs55437
 
Understanding-Objects-in-Javascript.pptx
MariaTrinidadTumanga
 
Ad

More from Bunlong Van (9)

PPT
scrummethodology-151002092252-lva1-app6891
Bunlong Van
 
PPT
Scrum methodology
Bunlong Van
 
PPT
Javascript dom event
Bunlong Van
 
PPT
Basic Javascript
Bunlong Van
 
PPT
Real time web and mobile application with Erlang & Ruby programming language
Bunlong Van
 
PPT
Why ruby?
Bunlong Van
 
PPT
Ruby on Rails testing with Rspec
Bunlong Van
 
PPT
How to develop app for facebook fan page
Bunlong Van
 
PPT
Why less?
Bunlong Van
 
scrummethodology-151002092252-lva1-app6891
Bunlong Van
 
Scrum methodology
Bunlong Van
 
Javascript dom event
Bunlong Van
 
Basic Javascript
Bunlong Van
 
Real time web and mobile application with Erlang & Ruby programming language
Bunlong Van
 
Why ruby?
Bunlong Van
 
Ruby on Rails testing with Rspec
Bunlong Van
 
How to develop app for facebook fan page
Bunlong Van
 
Why less?
Bunlong Van
 

Recently uploaded (20)

PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PDF
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
Presentation about variables and constant.pptx
safalsingh810
 
oapresentation.pptx
mehatdhavalrajubhai
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 

Javascript Object Oriented Programming

  • 1. Click to add Text Javascript Presented by Bunlong VAN http://geekhmer.github.io Object Oriented
  • 3. Object Oriented Basic •Object (instance): representation of a “thing” (someone or something) •Class: ablueprint, or recipe for an Object •Encapsulation: illustrates the fact that an Object contains (encapsulates): • Data (stored in properties) • The means to do something with data (using methods) •Inheritance
  • 4. Object Creation •Two simple ways var obj = new Object(); obj.name = “Foo”; obj.say = function() { return “Hello”; } var obj = { name: “Foo”, say: function() { return “Hello”; } }
  • 5. Method •When a property is a function we can call it a method var object = { method: function() { alert(“Here is method”); } }
  • 6. Prototype •JavaScript functions work as constructors, methods and modules •It has Prototypal Inheritance, which offers great expressive powers •All Objects are directly or indirectly link to Object.prototype •Each Object is linked to its parent via a magic link •When a member is accessed the compiler looks at the Object and then looks up to its parent via the magic link •It keeps going all the way up to Object.prototype •Go to console of firebug and type Object.prototype
  • 7. Prototype (Con.) •Looking for my_object.foo, found it in the Object itself •Looking for my_object.baz looks in the object and if it doesn't find it there it goes to my_object_parent which is my_object.prototype •Looking for my_object.some_random_member can't find it in the object, look at my_object_parent, can't find it •There either, goes to Object can't find it there and is set to undefined
  • 8. Prototype (Con.) var Person = function() { this.name = “Mr. Bunlong”; this.callMe = function() { alert(“I am ” + this.name); } } var personObj = new Person(); personObject.callMe(); personObject.constructor(); personObject.myFoo(); // will return underfined
  • 9. Object Augm entation •New members can be added to any object Person.prototype.callMeAgain = function() { alert(“I said my name is ” + this.name); }; personObj.callMeAgain();
  • 10. Prototype Usage •CallMeAgain() is a property of the prototype object •But it behaves as if it's a prototype of the dude object
  • 11. Own Properties Vs Prototype's •personObj.hasOwnProperty(“callMe”); // will return true •personObj.hasOwnProperty(“callMeAgain”); // will return false
  • 13. Inheritance •OOP ensures code reuse •Two forms of OOP • Classical with Mixin • Prototypal
  • 14. Prototypal Inheritance var Boy = function() {}; Boy.prototype = new Person(); var boyObj = new Boy(); boyObj.callMe();
  • 15. Inheritance through Mixin var Person = function(name) { this.greet = function() { alert(“Hello, I'm ” + name); } }; var Employee = function(name) { person.apply(this, [name]); this.getTeam = function() {return “UI Library”;} } // instantiate object of Employee var employee = new Employee(“Bunlong”); employee.greet(); var team = employee.getTeam(); alert(“Team: ” + team);
  • 16. Singleton •There is no need to produce a class like constructor for an object that will have exactly one instance •This is typical of JavaScript applications •Use an object literal to get started instead
  • 17. Module Pattern •The methods of a singleton can enjoy access to shared private datum and private methods •Variables defined in a module are only visible in a module •Variables defined in a function are only visible to the function •Function can be used as module containers
  • 18. Module Pattern (Con.) var myModule = function() { var privateVariable, privateFunction = function() { // coding here }; return { FirstMethod: function() { // can access privateVariable // can access privateFunction } } }(); myModule.firstMethod();