SlideShare a Scribd company logo
2
Most read
9
Most read
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

PPTX
тестирование по
Ion Griu
 
PPT
Microsoft excel-2007
MAXIMICH33
 
PPT
Advanced Javascript
Adieu
 
PDF
Testing Services - Software Quality Assurance
CCS Technologies (P) Ltd.
 
PDF
Analise de Requisitos Software
Rildo (@rildosan) Santos
 
PDF
Aula 02 - Engenharia de Requisitos
Alberto Simões
 
PDF
Istqb lesson 6
Eugene Bulba
 
PPSX
C# - Part 1
Md. Mahedee Hasan
 
тестирование по
Ion Griu
 
Microsoft excel-2007
MAXIMICH33
 
Advanced Javascript
Adieu
 
Testing Services - Software Quality Assurance
CCS Technologies (P) Ltd.
 
Analise de Requisitos Software
Rildo (@rildosan) Santos
 
Aula 02 - Engenharia de Requisitos
Alberto Simões
 
Istqb lesson 6
Eugene Bulba
 
C# - Part 1
Md. Mahedee Hasan
 

What's hot (20)

PPT
Software System Engineering - Chapter 1
Fadhil Ismail
 
PPT
Prototipação de software
leopp
 
PPT
Testes de Software
Capgemini
 
PPT
Test Automation Strategies For Agile
Naresh Jain
 
PPTX
Requirement Elicitation and Analysis.pptx
RojipRai
 
PPTX
ISTQB foundation level - day 2
Shuchi Singla AKT,SPC4,PMI-ACP,ITIL(F),CP-AAT
 
PPT
Prototipagem
Robson Santos
 
PPT
Introdução a Automação de Teste de Software
Camilo Ribeiro
 
PDF
Working With Legacy Code
Andrea Polci
 
PPTX
ISO 15504
Marcelo Ramos
 
PDF
Aula 02-processos-e-threads-tanenbaum-parte-2
Cristiano Pires Martins
 
PDF
Istqb lesson 1
Eugene Bulba
 
PDF
CON08 - VLAN.pdf
Silvano Oliveira
 
PDF
New trends in testing automation
Eran Kinsbrunner
 
PDF
ITIL V3
Carlos Domingues
 
PPSX
Junit
FAROOK Samath
 
PPT
X-Zone - Garantia da Qualidade de Software
AlexandreBartie
 
PDF
TDD Flow: The Mantra in Action
Dionatan default
 
PPTX
UNIT TESTING PPT
suhasreddy1
 
PDF
自殺学 第3回 自殺生起過程
Hajime SUEKI
 
Software System Engineering - Chapter 1
Fadhil Ismail
 
Prototipação de software
leopp
 
Testes de Software
Capgemini
 
Test Automation Strategies For Agile
Naresh Jain
 
Requirement Elicitation and Analysis.pptx
RojipRai
 
ISTQB foundation level - day 2
Shuchi Singla AKT,SPC4,PMI-ACP,ITIL(F),CP-AAT
 
Prototipagem
Robson Santos
 
Introdução a Automação de Teste de Software
Camilo Ribeiro
 
Working With Legacy Code
Andrea Polci
 
ISO 15504
Marcelo Ramos
 
Aula 02-processos-e-threads-tanenbaum-parte-2
Cristiano Pires Martins
 
Istqb lesson 1
Eugene Bulba
 
CON08 - VLAN.pdf
Silvano Oliveira
 
New trends in testing automation
Eran Kinsbrunner
 
X-Zone - Garantia da Qualidade de Software
AlexandreBartie
 
TDD Flow: The Mantra in Action
Dionatan default
 
UNIT TESTING PPT
suhasreddy1
 
自殺学 第3回 自殺生起過程
Hajime SUEKI
 
Ad

Viewers also liked (7)

PDF
Javascript cheat-sheet-v1
hccit
 
PDF
Object Oriented Programming in JavaScript
zand3rs
 
PDF
Introduction to Object Oriented Javascript
nodeninjas
 
ODP
JavaScript global object, execution contexts & closures
HDR1001
 
PPTX
Grunt
Dohoon Kim
 
PPTX
Javascript Prototype Visualized
军 沈
 
PPT
JavaScript & Dom Manipulation
Mohammed Arif
 
Javascript cheat-sheet-v1
hccit
 
Object Oriented Programming in JavaScript
zand3rs
 
Introduction to Object Oriented Javascript
nodeninjas
 
JavaScript global object, execution contexts & closures
HDR1001
 
Grunt
Dohoon Kim
 
Javascript Prototype Visualized
军 沈
 
JavaScript & Dom Manipulation
Mohammed Arif
 
Ad

Similar to JavaScript Object Oriented Programming Cheat Sheet (20)

PPTX
Object oriented javascript
Usman Mehmood
 
PDF
The prototype property
Hernan Mammana
 
PPTX
Object oriented programming in JavaScript
Aditya Majety
 
PPTX
Function-and-prototype defined classes in JavaScript
Hong Langford
 
PPT
Advanced Javascript
Manikanda kumar
 
PPT
Advanced Javascript
relay12
 
PPTX
Js: master prototypes
Barak Drechsler
 
PPT
Advanced JavaScript
Stoyan Stefanov
 
KEY
2012 oct-12 - java script inheritance
pedro.carvalho
 
PPTX
Javascript Prototypal Inheritance - Big Picture
Manish Jangir
 
PPTX
Understanding-Objects-in-Javascript.pptx
MariaTrinidadTumanga
 
PDF
javascript objects
Vijay Kalyan
 
PDF
Prototype 120102020133-phpapp02
plutoone TestTwo
 
PDF
JavaScript Essentials
Triphon Statkov
 
PDF
Javascript under the hood 2
Thang Tran Duc
 
PDF
Java script object model
James Hsieh
 
PDF
JavaScript Inheritance
Jussi Pohjolainen
 
PDF
Prototype
Aditya Gaur
 
PPT
Javascript Object Oriented Programming
Bunlong Van
 
PPTX
Oojs 1.1
Rodica Dada
 
Object oriented javascript
Usman Mehmood
 
The prototype property
Hernan Mammana
 
Object oriented programming in JavaScript
Aditya Majety
 
Function-and-prototype defined classes in JavaScript
Hong Langford
 
Advanced Javascript
Manikanda kumar
 
Advanced Javascript
relay12
 
Js: master prototypes
Barak Drechsler
 
Advanced JavaScript
Stoyan Stefanov
 
2012 oct-12 - java script inheritance
pedro.carvalho
 
Javascript Prototypal Inheritance - Big Picture
Manish Jangir
 
Understanding-Objects-in-Javascript.pptx
MariaTrinidadTumanga
 
javascript objects
Vijay Kalyan
 
Prototype 120102020133-phpapp02
plutoone TestTwo
 
JavaScript Essentials
Triphon Statkov
 
Javascript under the hood 2
Thang Tran Duc
 
Java script object model
James Hsieh
 
JavaScript Inheritance
Jussi Pohjolainen
 
Prototype
Aditya Gaur
 
Javascript Object Oriented Programming
Bunlong Van
 
Oojs 1.1
Rodica Dada
 

Recently uploaded (20)

PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PPTX
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
PDF
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 

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

  • #5: //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); }