SlideShare a Scribd company logo
7
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

What's hot (20)

PDF
Domain Modeling Made Functional (DevTernity 2022)
Scott Wlaschin
 
PPTX
Javascript - Array - Creating Array
Samuel Santos
 
PDF
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
MongoDB
 
PPTX
Why TypeScript?
FITC
 
PDF
Redis
DaeMyung Kang
 
ODP
Patroni: PostgreSQL HA in the cloud
Lucio Grenzi
 
PPTX
保哥線上講堂:LINQ 快速上手
Will Huang
 
PDF
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Edureka!
 
PDF
Replication Troubleshooting in Classic VS GTID
Mydbops
 
PDF
Introduction to SOLID Principles
Ganesh Samarthyam
 
PDF
The Art of Unit Testing - Towards a Testable Design
Victor Rentea
 
PDF
애플리케이션 아키텍처와 객체지향
Young-Ho Cho
 
PDF
ORM: Object-relational mapping
Abhilash M A
 
PPTX
React.js - The Dawn of Virtual DOM
Jimit Shah
 
PDF
Ruby on Rails Presentation
adamcookeuk
 
PDF
Lessons Learned: Troubleshooting Replication
Sveta Smirnova
 
PDF
MySQL Data Encryption at Rest
Mydbops
 
PDF
TypeScript - An Introduction
NexThoughts Technologies
 
PPTX
MySQL8.0_performance_schema.pptx
NeoClova
 
PDF
Javascript basic course
Tran Khoa
 
Domain Modeling Made Functional (DevTernity 2022)
Scott Wlaschin
 
Javascript - Array - Creating Array
Samuel Santos
 
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
MongoDB
 
Why TypeScript?
FITC
 
Patroni: PostgreSQL HA in the cloud
Lucio Grenzi
 
保哥線上講堂:LINQ 快速上手
Will Huang
 
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Edureka!
 
Replication Troubleshooting in Classic VS GTID
Mydbops
 
Introduction to SOLID Principles
Ganesh Samarthyam
 
The Art of Unit Testing - Towards a Testable Design
Victor Rentea
 
애플리케이션 아키텍처와 객체지향
Young-Ho Cho
 
ORM: Object-relational mapping
Abhilash M A
 
React.js - The Dawn of Virtual DOM
Jimit Shah
 
Ruby on Rails Presentation
adamcookeuk
 
Lessons Learned: Troubleshooting Replication
Sveta Smirnova
 
MySQL Data Encryption at Rest
Mydbops
 
TypeScript - An Introduction
NexThoughts Technologies
 
MySQL8.0_performance_schema.pptx
NeoClova
 
Javascript basic course
Tran Khoa
 

Viewers also liked (6)

PDF
Javascript cheat-sheet-v1
hccit
 
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
 
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
relay12
 
PPT
Advanced Javascript
Manikanda kumar
 
PPT
Advanced Javascript
Adieu
 
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
 
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
relay12
 
Advanced Javascript
Manikanda kumar
 
Advanced Javascript
Adieu
 
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
 
Ad

Recently uploaded (20)

PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 

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