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
Object Oriented Programming In JavaScript
PDF
Introduction to Object Oriented Javascript
ODP
JavaScript global object, execution contexts & closures
PDF
JavaScript Execution Context
PDF
JavaScript: Patterns, Part 3
ODP
Introduction of Object Oriented JavaScript
PDF
JavaScript: Patterns, Part 2
PDF
Oslo.versioned objects - Deep Dive
Object Oriented Programming In JavaScript
Introduction to Object Oriented Javascript
JavaScript global object, execution contexts & closures
JavaScript Execution Context
JavaScript: Patterns, Part 3
Introduction of Object Oriented JavaScript
JavaScript: Patterns, Part 2
Oslo.versioned objects - Deep Dive

What's hot (20)

PPTX
Declarative JavaScript concepts and implemetation
PPTX
JavaScript Beyond jQuery
PPTX
Functions and Objects in JavaScript
PPTX
Advanced Javascript
PDF
JavaScript objects and functions
PPTX
An introduction to Object Oriented JavaScript
PDF
JavaScript Objects
PPTX
Qtp training session IV
PPTX
Groovy / comparison with java
PDF
JavaScript - Chapter 5 - Operators
PDF
Javascript for Intermediates
PPTX
JavaScript operators
PDF
Advanced JavaScript - Internship Presentation - Week6
PPTX
JavsScript OOP
PPTX
Exploring Kotlin language basics for Android App development
PPT
JavaScript Operators
PPTX
JavaScript Basics
PPTX
Introduction to JavaScript
ODP
Functors, Applicatives and Monads In Scala
PPTX
Constructor & destructor
Declarative JavaScript concepts and implemetation
JavaScript Beyond jQuery
Functions and Objects in JavaScript
Advanced Javascript
JavaScript objects and functions
An introduction to Object Oriented JavaScript
JavaScript Objects
Qtp training session IV
Groovy / comparison with java
JavaScript - Chapter 5 - Operators
Javascript for Intermediates
JavaScript operators
Advanced JavaScript - Internship Presentation - Week6
JavsScript OOP
Exploring Kotlin language basics for Android App development
JavaScript Operators
JavaScript Basics
Introduction to JavaScript
Functors, Applicatives and Monads In Scala
Constructor & destructor
Ad

Similar to JavaScript Object Oriented Programming Cheat Sheet (20)

PPTX
Object oriented javascript
PDF
The prototype property
PPTX
Object oriented programming in JavaScript
PPTX
Function-and-prototype defined classes in JavaScript
PPT
Advanced Javascript
PPT
Advanced Javascript
PPT
Advanced Javascript
PPTX
Js: master prototypes
PPT
Advanced JavaScript
KEY
2012 oct-12 - java script inheritance
PPTX
Javascript Prototypal Inheritance - Big Picture
PPTX
Understanding-Objects-in-Javascript.pptx
PDF
javascript objects
PPTX
Javascript Prototype Visualized
PDF
Prototype 120102020133-phpapp02
PDF
JavaScript Essentials
PDF
Javascript under the hood 2
PDF
Java script object model
PDF
JavaScript Inheritance
PDF
Prototype
Object oriented javascript
The prototype property
Object oriented programming in JavaScript
Function-and-prototype defined classes in JavaScript
Advanced Javascript
Advanced Javascript
Advanced Javascript
Js: master prototypes
Advanced JavaScript
2012 oct-12 - java script inheritance
Javascript Prototypal Inheritance - Big Picture
Understanding-Objects-in-Javascript.pptx
javascript objects
Javascript Prototype Visualized
Prototype 120102020133-phpapp02
JavaScript Essentials
Javascript under the hood 2
Java script object model
JavaScript Inheritance
Prototype
Ad

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPT
Teaching material agriculture food technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
MIND Revenue Release Quarter 2 2025 Press Release
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
Network Security Unit 5.pdf for BCA BBA.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
Spectral efficient network and resource selection model in 5G networks
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Review of recent advances in non-invasive hemoglobin estimation
20250228 LYD VKU AI Blended-Learning.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Teaching material agriculture food technology

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); }