SlideShare a Scribd company logo
1 of 12
Object Oriented Programming
in
JavaScript

© Hans de Rooij
hdr.is-a-geek.com
JavaScript object basics
●

●

In JavaScript an object is an (unordered)
collection of name-value pairs
Please note that in JavaScript values can be
functions!
Built-in objects
●

Built-in objects (with constructor)
–

●

Built-in objects (without constructor)
–

●

Object, Function, Array, Date, RegExp & Error
Global object, Math & JSON

Primitive wrapper objects
–

String, Number & Boolean
Built-in object creation
●

Objects are created using constructors in new
expressions
–

●

●

Alternative syntax is the object literal

Objects without constructors can be used
immediately. For instance: x = Math.PI;
Wrapper objects are created automatically
when needed

JavaScript built-in objects fiddle
Custom object creation
Custom objects can be created using a
●

●

… regular JavaScript function that returns an
object reference
… constructor function invoked in the context
of a new expression
Please note;
–

A constructor is a regular JavaScript function!

–

In case no prototype functionality is used all properties and methods will be
created on the object instance

JavaScript custom objects fiddle
Constructor function & prototype
●

Using both the constructor function and
prototype it's possible to define
–
–

●

●

object state at the instance level and
object behavior at the level of the shared prototype

When reading JavaScript properties the entire
prototype chain is searched in case a property
cannot be located on an object instance
Individual object instances can override
inherited behavior

JavaScript constructor function and prototype fiddle
schema on next slide!
Constructor prototype
●

Schematically
JavaScript inheritance
●

Combination inheritance
–

is the most common way to implement inheritance
in JavaScript

–

has two components
●

●

Prototype chaining for the implementation of (shared)
base class behavior
Constructor stealing for the initialization of base class
properties on derived object instances

JavaScript combination inheritance fiddle
schema on next slide!
JavaScript inheritance example
Prototypal inheritance
●

In prototypal inheritance
–

–

●

a new object instance inherits directly from
another object instance
there is no need to implement constructor
functions

In ECMAScript (fifth edition) prototypal
inheritance is implemented as follows:
var die_3 = Object.create(die_1);

JavaScript prototypal inheritance fiddle
schema on next slide!
Prototypal inheritance example
Conclusion
●

●

●

●

JavaScript has strong capabilities in the area
of Object Oriented Programming
The implementation of OOP features differs
greatly from class based languages
In JavaScript it's common for there to be
several OOP implementation alternatives
I had to invest a significant amount of time &
effort to come to grips with all the intricacies of
JavaScript OOP but, in the end, it proved to be
a great learning experience for me
© Hans de Rooij

hdr.is-a-geek.com

More Related Content

What's hot

Javascript built in String Functions
Javascript built in String FunctionsJavascript built in String Functions
Javascript built in String Functions
Avanitrambadiya
 

What's hot (20)

ASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with Overview
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Java Web Services [1/5]: Introduction to Web Services
Java Web Services [1/5]: Introduction to Web ServicesJava Web Services [1/5]: Introduction to Web Services
Java Web Services [1/5]: Introduction to Web Services
 
ESTEM_ReactJS_S1.pptx
ESTEM_ReactJS_S1.pptxESTEM_ReactJS_S1.pptx
ESTEM_ReactJS_S1.pptx
 
Javascript
JavascriptJavascript
Javascript
 
Javascript built in String Functions
Javascript built in String FunctionsJavascript built in String Functions
Javascript built in String Functions
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Modern JS with ES6
Modern JS with ES6Modern JS with ES6
Modern JS with ES6
 
Closures in Javascript
Closures in JavascriptClosures in Javascript
Closures in Javascript
 
Soap web service
Soap web serviceSoap web service
Soap web service
 
ASP.NET - Life cycle of asp
ASP.NET - Life cycle of aspASP.NET - Life cycle of asp
ASP.NET - Life cycle of asp
 
Expressjs
ExpressjsExpressjs
Expressjs
 
Presentation of framework Angular
Presentation of framework AngularPresentation of framework Angular
Presentation of framework Angular
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Web Services and Introduction of SOAPUI
Web Services and Introduction of SOAPUIWeb Services and Introduction of SOAPUI
Web Services and Introduction of SOAPUI
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6
 
Getting started with entity framework
Getting started with entity framework Getting started with entity framework
Getting started with entity framework
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorial
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 

Viewers also liked (7)

Javascript cheat-sheet-v1
Javascript cheat-sheet-v1Javascript cheat-sheet-v1
Javascript cheat-sheet-v1
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
Introduction to Object Oriented Javascript
Introduction to Object Oriented JavascriptIntroduction to Object Oriented Javascript
Introduction to Object Oriented Javascript
 
JavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closuresJavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closures
 
Grunt
GruntGrunt
Grunt
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 

Similar to JavaScript Object Oriented Programming Cheat Sheet

JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
Chamnap Chhorn
 
Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02
Sopheak Sem
 

Similar to JavaScript Object Oriented Programming Cheat Sheet (20)

JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Javascript omg!
Javascript omg!Javascript omg!
Javascript omg!
 
Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
Javascript
JavascriptJavascript
Javascript
 
Post-graduate course: Object technology: Prototype-based object-oriented prog...
Post-graduate course: Object technology: Prototype-based object-oriented prog...Post-graduate course: Object technology: Prototype-based object-oriented prog...
Post-graduate course: Object technology: Prototype-based object-oriented prog...
 
Java scriptforjavadev part2a
Java scriptforjavadev part2aJava scriptforjavadev part2a
Java scriptforjavadev part2a
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
New c sharp4_features_part_v
New c sharp4_features_part_vNew c sharp4_features_part_v
New c sharp4_features_part_v
 
Post-graduate course: Object technology: Implementation of object-oriented pr...
Post-graduate course: Object technology: Implementation of object-oriented pr...Post-graduate course: Object technology: Implementation of object-oriented pr...
Post-graduate course: Object technology: Implementation of object-oriented pr...
 
Prototype Object.pptx
Prototype Object.pptxPrototype Object.pptx
Prototype Object.pptx
 
Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big Picture
 
Extending Node.js using C++
Extending Node.js using C++Extending Node.js using C++
Extending Node.js using C++
 
Js: master prototypes
Js: master prototypesJs: master prototypes
Js: master prototypes
 
Chapter 1- Introduction.ppt
Chapter 1- Introduction.pptChapter 1- Introduction.ppt
Chapter 1- Introduction.ppt
 
Object Oriented Javascript
Object Oriented JavascriptObject Oriented Javascript
Object Oriented Javascript
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 

JavaScript Object Oriented Programming Cheat Sheet

  • 1. Object Oriented Programming in JavaScript © Hans de Rooij hdr.is-a-geek.com
  • 2. JavaScript object basics ● ● In JavaScript an object is an (unordered) collection of name-value pairs Please note that in JavaScript values can be functions!
  • 3. Built-in objects ● Built-in objects (with constructor) – ● Built-in objects (without constructor) – ● Object, Function, Array, Date, RegExp & Error Global object, Math & JSON Primitive wrapper objects – String, Number & Boolean
  • 4. Built-in object creation ● Objects are created using constructors in new expressions – ● ● Alternative syntax is the object literal Objects without constructors can be used immediately. For instance: x = Math.PI; Wrapper objects are created automatically when needed JavaScript built-in objects fiddle
  • 5. Custom object creation Custom objects can be created using a ● ● … regular JavaScript function that returns an object reference … constructor function invoked in the context of a new expression Please note; – A constructor is a regular JavaScript function! – In case no prototype functionality is used all properties and methods will be created on the object instance JavaScript custom objects fiddle
  • 6. Constructor function & prototype ● Using both the constructor function and prototype it's possible to define – – ● ● object state at the instance level and object behavior at the level of the shared prototype When reading JavaScript properties the entire prototype chain is searched in case a property cannot be located on an object instance Individual object instances can override inherited behavior JavaScript constructor function and prototype fiddle schema on next slide!
  • 8. JavaScript inheritance ● Combination inheritance – is the most common way to implement inheritance in JavaScript – has two components ● ● Prototype chaining for the implementation of (shared) base class behavior Constructor stealing for the initialization of base class properties on derived object instances JavaScript combination inheritance fiddle schema on next slide!
  • 10. Prototypal inheritance ● In prototypal inheritance – – ● a new object instance inherits directly from another object instance there is no need to implement constructor functions In ECMAScript (fifth edition) prototypal inheritance is implemented as follows: var die_3 = Object.create(die_1); JavaScript prototypal inheritance fiddle schema on next slide!
  • 12. Conclusion ● ● ● ● JavaScript has strong capabilities in the area of Object Oriented Programming The implementation of OOP features differs greatly from class based languages In JavaScript it's common for there to be several OOP implementation alternatives I had to invest a significant amount of time & effort to come to grips with all the intricacies of JavaScript OOP but, in the end, it proved to be a great learning experience for me © Hans de Rooij hdr.is-a-geek.com

Editor's Notes

  1. //Declaration object references var obj_inst_1, obj_inst_2; //Use the Object constructor in conjunction with the new operator to //instantiate a new object and assign the reference to obj_inst_1 obj_inst_1 = new Object(); //In JavaScript object properties and methods can be added on the fly obj_inst_1.prop = "Added on the fly"; obj_inst_1.func = function() {return this.prop + "!";}; toFiddleResult(obj_inst_1.func()); //Object literal, an alternative way to instantiate Javascript objects obj_inst_2 = { prop: "Added as part of the object literal", func: function() {return this.prop + "!";} }; toFiddleResult(obj_inst_2.func()); //There is no need to instantiate objects without constructors, i.e. //the global object, Math & JSON var global = this; //In top level code this refers to the global object toFiddleResult("Is 42 a finite number? " + global.isFinite(42)); toFiddleResult("The number u03c0 = " + Math.PI); //Primitive wrapper objects are created automatically when needed var str = "u00a9 Hans de Rooij"; toFiddleResult(str.substring(2, 6) + " wrote this code"); //Function for displaying code results in the JSFiddle results pane function toFiddleResult(str) { //Reference to document node containing fiddle results var result_div = document.getElementById("fiddle_result"); //Fiddle results are listed in pre tags var add_pre = document.createElement("pre"); add_pre.appendChild(document.createTextNode(str)); result_div.appendChild(add_pre); }