SlideShare a Scribd company logo
JavaScript OOPs
2014/01/22

Johnson
OOP
●

Inheritance
○
○

●

An object is based on another object using the same implementation
Code reuse

Encapsulation
○
○

●

Restricting access to some of the object's components
Code extensibility

Polymorphism
○

A single interface to entities of different types

○

Code flexibiity
Classical vs Prototypal Inheritance
●

Classical
○
○

●

In most OOP languages, there are classes and objects
Classes inherit from other classes

Prototypal
○

JavaScript is prototypal based

○

An object inherits from another object
Classical vs Prototypal Inheritance
[[Object]]
Animal

[[CLASS]]
Animal

[[Object]]

[[Object]]

Cat

Cat

[[Object]]

[[Object]]

Dog

Dog
Peudoclassical
●

Contructor Pattern
○

Using the new keyword

○

Using this to store object own status

○

Using prototype to share methods
Pesudoclassical
●

Instantiation

1 function Rectangle(width, height) {
2
this.height = height;
3
this.width = width;
4}
5
6 Rectangle.prototype.area = function () {
7
return this.width * this.height;
8 };
9
10 var rect = new Rectangle(5, 10);
Pesudoclassical
●

Subclass Inheritance

1 function Square(side) {
2
Rectangle.call(this, side, side);
3}
4
5 Square.prototype = Object.create(Rectangle.prototype);
6
7 Square.prototype.constructor = Square;
8
9 var sq = new Square(5);
10
11 alert(sq.area());
Pesudoclassical
●

Encapsulation & Privileged Method

1 function Game() {
2
var name = ‘U3D’;
3
var version = 0.5;
4
this.getVersion = function () {
5
return ‘Version: ’ + name + ‘ - ’ + version;
6
}
7}
8 Game.prototype.play = function () { return ‘play’; };
9
10 var g = new Game();
11 alert(g.getVersion());
Pesudoclassical
●
1
2
3
4
5
6
7
8
9

Polymorphism

function SpaceGame() {
Game.call(this);
}
...
SpaceGame.prototype.play = function () { return ‘space play’; };
var sg = new SpaceGame();
alert(sg.play());
Comparison With Prototypal
●
1
2
3
4
5
6
7
8

Instantiation

var rect = {
height: 5,
width: 10
}
rect.area = function () {
return this.width * this.height;
};

1 function Rectangle(width, height) {
2
this.height = height;
3
this.width = width;
4}
5
6 Rectangle.prototype.area = function () {
7
return this.width * this.height;
8 };
9
10 var rect = new Rectangle(5, 10);
Comparison With Prototypal
●
1
2
3
4
5
6

Subclass Inheritance

var sq = Object.create(rect);
sq.height = 5;
sq.width = 5;
alert(sq.area());

1 function Square(side) {
2
Rectangle.call(this, side, side);
3}
4
5 Square.prototype = Object.create(Rectangle.prototype);
6
7 Square.prototype.constructor = Square;
8
9 var sq = new Square(5);
10
11 alert(sq.area());
Comparison With Prototypal
●

Encapsulation & Privileged Method

1 var g = {};
2 // Imediate function & closure
3 g.getVersion = (function() {
4
var name = ‘U3D’;
5
var version = 0.5;
6
return function() {
7
return ‘Version: ’ + name + ‘ - ’ + version;
8
}
9 })();
10
11 alert(g.getVersion());
Comparison With Prototypal
●
1
2
3
4
5
6
7

Polymorphism

var sg = Object.create(g);
sg.play = function() {
return ‘space play’;
};
alert(sg.play());

1 function SpaceGame() {
2
Game.call(this);
3}
4 ...
5 SpaceGame.prototype.play = function () {
6
return ‘space play’;
7 };
8
9 var sg = new SpaceGame();
10
11 alert(sg.play());
Stop Using The new Keyword
●

Raynos
new is a remnant of the days where JavaScript accepted
a Java like syntax for gaining “popularity”

●

Brendan Eich
And we were pushing it as a little brother to Java, as
a complementary language like Visual Basic was to C++
in Microsoft’s language families at the time
Stop Using The new Keyword
●

Douglas Crockford
This indirection was intended to make the language seem
more familiar to classically trained programmers, but
failed to do that, as we can see from the very low
opinion
Java
programmers
have
of
JavaScript.
JavaScript’s constructor pattern did not appeal to the
classical crowd. It also obscured JavaScript’s true
prototypal nature. As a result, there are very few
programmers
who
know
how
to
use
the
language
effectively.
Advantages of Prototypal Pattern
Constructor Pattern

Prototypal Pattern

Functional features can't be used in
conjunction with the new keyword.

Functional features can be used in conjunction
with create.

Forgetting to use new leads to unexpected
bugs and global variables.

Since create is a function the program will
always work as expected.

Prototypal inheritance is unnecessarily
complicated and confusing.

Prototypal inheritance is simple and easy to
understand.
Two Types of Prototype-based Language
●

Delegation
In prototype-based languages that use delegation, the
language runtime is capable of dispatching the correct
method or finding the right piece of data simply by
following a series of delegation pointers (from object
to its prototype) until a match is found.

●

Concatenation
… there are no visible pointers or links to the
original prototype from which an object is cloned. The
prototype (parent) object is copied rather than linked
to. As a result, changes to the prototype will not be
reflected in cloned objects.
Delegation vs Concatenation Inheritance
[[Object]]
[[Object]]
Animal
Animal

[[Object]]

[[Object]]

Cat

Cat

[[Object]]
Dog

[[Object]]
Dog
Pros and Cons
Delegation

Concatenation

Any changes to the prototype are automatically
reflected on all its clones.

Any changes to the prototype need to be
propagated to all its clones.

Property access is slower because it may need
to traverse up the prototype chain.

Property access is faster because inherited
properties are copied.

Objects may only delegate to a single prototype
in JavaScript.

Objects may copy properties from any number
of prototypes.
Functional Pattern(Factory Pattern)
●

Douglas Crockford
The functional pattern has a great deal of flexibility.
It requires less effort than the pseudoclassical
pattern, and gives us better encapsulation and
information hiding and access to super methods.

●

GoF
Favor object composition over class inheritance
Functional Pattern
●

Instantiation

1 function rectangle(spec) {
2
var that = {};
3
that.height = spec.height;
4
that.width = spec.width;
5
that.area = function() { return spec.width * spec.height; };
5
return that;
6}
7
8 var rect = rectangle({height: 5, width: 10});
Functional Pattern
●
1
2
3
4
5
6
7

Subclass Inheritance

function square(spec) {
var that = rectangle({height: spec.side, width: spec.side});
return that;
}
var sq = square({side: 5});
alert(sq.area());
Functional Pattern
●

Encapsulation & Privileged Method

1 function square(spec) {
2
var that = rectangle({height: spec.side, width: spec.side});
3
4
var varsion = ‘1.0’;
5
that.getVersion = function() { return ‘Version: ’ + version; };
6
7
return that;
8}
9
10 var sq = square({side: 5});
11 alert(sq.getVersion());
Functional Pattern
●
1
2
3
4
5
6
7
8

Polymorphism

function square(spec) {
var that = rectangle({height: spec.side, width: spec.side});
that.area = function(){ return spec.width * spec.height * 5; }
return that;
}
var sq = square({side: 5});
alert(sq.area());
Benchmark

Constructor v.s. Prototypal v.s. Functional
Reference
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●

Benefits of prototypal inheritance over classical
Why prototypal inheritance matters?
Prototypal-based programming
Prototypal Inheritance in JavaScript
Stop Using Constructor Functions In JavaScript
Fluent JavaScript - Three Different Kinds of Prototypal OO
Classical Inheritance Is Obsolete - How To Think In Prototypal OO
Mixin Pattern In JavaScript
JavaScript Module Pattern: In-Depth
Prototype-based Programming
Delegation Programming
Dynamic Dispatch
What The Fuck Is Prototypal Inheritance?
Introduction to Object-Oriented JavaScript
Inheritance and Prototype Chain
Prototypal Inheritance
Comparing Prototypal and Classical Inheritance
Classical vs Prototypal Inheritance
JavaScript Prototypal Object Oriented Programming in Practice
Make Object-Oriented Programming Easier With Only Six Lines Of JavaScript
Reference
●
●
●
●
●
●
●
●
●
●

JavaScript Constructor Prototypes and The new Keyword
Create Advanced Web Applications With Object-Oriented Techniques
Scripting Mantainability
Optimizing JavaScript for Extreme Performance and Low Memory Consumption
OO Design Patterns
JavaScript Is Sexy
JavaScript Inheritance Patterns
JavaScript: The Good Parts
Secrets of The JavaScript Ninja
JavaScript: The Definitive Guide, 6th Edition

More Related Content

What's hot (20)

PPTX
Lecture 5 javascript
Mujtaba Haider
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPTX
Javascript basics for automation testing
Vikas Thange
 
PDF
Advanced JavaScript - Internship Presentation - Week6
Devang Garach
 
PPT
Advanced JavaScript
Fu Cheng
 
PPTX
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
raja kvk
 
PDF
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
PDF
Performance Optimization and JavaScript Best Practices
Doris Chen
 
PPT
Advanced JavaScript
Stoyan Stefanov
 
PDF
Java Script Best Practices
Enrique Juan de Dios
 
PPT
Advanced Javascript
Adieu
 
PDF
Basics of JavaScript
Bala Narayanan
 
PDF
JavaScript 101
ygv2000
 
ODP
Javascript
theacadian
 
PPTX
JavaScript in Object-Oriented Way
Chamnap Chhorn
 
PDF
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
Edureka!
 
PPTX
Lab #2: Introduction to Javascript
Walid Ashraf
 
PDF
JavaScript - From Birth To Closure
Robert Nyman
 
PPT
JavaScript - An Introduction
Manvendra Singh
 
Lecture 5 javascript
Mujtaba Haider
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Javascript basics for automation testing
Vikas Thange
 
Advanced JavaScript - Internship Presentation - Week6
Devang Garach
 
Advanced JavaScript
Fu Cheng
 
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
raja kvk
 
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
Performance Optimization and JavaScript Best Practices
Doris Chen
 
Advanced JavaScript
Stoyan Stefanov
 
Java Script Best Practices
Enrique Juan de Dios
 
Advanced Javascript
Adieu
 
Basics of JavaScript
Bala Narayanan
 
JavaScript 101
ygv2000
 
Javascript
theacadian
 
JavaScript in Object-Oriented Way
Chamnap Chhorn
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
Edureka!
 
Lab #2: Introduction to Javascript
Walid Ashraf
 
JavaScript - From Birth To Closure
Robert Nyman
 
JavaScript - An Introduction
Manvendra Singh
 

Viewers also liked (20)

PDF
Prototypal inheritance in JavaScript
Miroslav Obradović
 
PPTX
Listening barriers
mbrownpartin
 
PPT
Retooling of Social Studies
Springport Public Schools
 
PDF
UKLUG 2012 Leverage user adoption with gamification
Sasja Beerendonk
 
PDF
Deep diveconnections eco systeem
Sasja Beerendonk
 
PDF
BP305 Show me the money! The value in social business
Sasja Beerendonk
 
PDF
Connect, it WorX for secretaries
Sasja Beerendonk
 
PDF
Internet Week Yahoo! Academy: Working the Digital Canvas
YahooUK
 
PDF
Introduction to omap4 pad configuration
Johnson Chou
 
PDF
[Webinar] Office 365:Slimmer samenwerken in een project
Sasja Beerendonk
 
PDF
User Adoption Strategies - 2 days workshops @Silverside
Sasja Beerendonk
 
PDF
CSS Sanity with Sass: The Inverted Triangle Approach
Julie Kuehl
 
PDF
Prototype
Aditya Gaur
 
PPTX
SASS - CSS with Superpower
Kanchha kaji Prajapati
 
KEY
Advanced sass/compass
Nick Cooley
 
PDF
Change behavior, one Tiny Habit at a time
Sasja Beerendonk
 
PPTX
Javascript Prototype Visualized
军 沈
 
PDF
[Webinar] Office 365: Slimmer vergaderen
Sasja Beerendonk
 
PDF
Smart CSS theming with Sass and Compass
Mihaela
 
PPTX
Syntactically awesome stylesheets (Sass)
Tahmina Khatoon
 
Prototypal inheritance in JavaScript
Miroslav Obradović
 
Listening barriers
mbrownpartin
 
Retooling of Social Studies
Springport Public Schools
 
UKLUG 2012 Leverage user adoption with gamification
Sasja Beerendonk
 
Deep diveconnections eco systeem
Sasja Beerendonk
 
BP305 Show me the money! The value in social business
Sasja Beerendonk
 
Connect, it WorX for secretaries
Sasja Beerendonk
 
Internet Week Yahoo! Academy: Working the Digital Canvas
YahooUK
 
Introduction to omap4 pad configuration
Johnson Chou
 
[Webinar] Office 365:Slimmer samenwerken in een project
Sasja Beerendonk
 
User Adoption Strategies - 2 days workshops @Silverside
Sasja Beerendonk
 
CSS Sanity with Sass: The Inverted Triangle Approach
Julie Kuehl
 
Prototype
Aditya Gaur
 
SASS - CSS with Superpower
Kanchha kaji Prajapati
 
Advanced sass/compass
Nick Cooley
 
Change behavior, one Tiny Habit at a time
Sasja Beerendonk
 
Javascript Prototype Visualized
军 沈
 
[Webinar] Office 365: Slimmer vergaderen
Sasja Beerendonk
 
Smart CSS theming with Sass and Compass
Mihaela
 
Syntactically awesome stylesheets (Sass)
Tahmina Khatoon
 
Ad

Similar to JavaScript OOPs (20)

PPTX
Ajaxworld
deannalagason
 
PPT
Introduction to Javascript
Amit Tyagi
 
PDF
Java design patterns
Shawn Brito
 
PPTX
JavaScript (without DOM)
Piyush Katariya
 
PDF
Java Script Workshop
Dmitry Baranovskiy
 
PPTX
2. Design patterns. part #2
Leonid Maslov
 
PPTX
Modeling Patterns for JavaScript Browser-Based Games
Ray Toal
 
PDF
Having Fun with Kotlin Android - DILo Surabaya
DILo Surabaya
 
PDF
Oscon Java Testing on the Fast Lane
Andres Almiray
 
PPTX
Clojure And Swing
Skills Matter
 
PDF
PHP 8: Process & Fixing Insanity
GeorgePeterBanyard
 
PDF
Douglas Crockford: Serversideness
WebExpo
 
PPTX
oojs
Imran shaikh
 
PPT
What's New in Groovy 1.6?
Guillaume Laforge
 
PDF
Apache Groovy's Metaprogramming Options and You
Andres Almiray
 
KEY
QwalKeko, a History Querying Tool
stevensreinout
 
PDF
Design patterns
Anas Alpure
 
PPTX
JavaScript Beyond jQuery
Bobby Bryant
 
PDF
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
Codemotion
 
Ajaxworld
deannalagason
 
Introduction to Javascript
Amit Tyagi
 
Java design patterns
Shawn Brito
 
JavaScript (without DOM)
Piyush Katariya
 
Java Script Workshop
Dmitry Baranovskiy
 
2. Design patterns. part #2
Leonid Maslov
 
Modeling Patterns for JavaScript Browser-Based Games
Ray Toal
 
Having Fun with Kotlin Android - DILo Surabaya
DILo Surabaya
 
Oscon Java Testing on the Fast Lane
Andres Almiray
 
Clojure And Swing
Skills Matter
 
PHP 8: Process & Fixing Insanity
GeorgePeterBanyard
 
Douglas Crockford: Serversideness
WebExpo
 
What's New in Groovy 1.6?
Guillaume Laforge
 
Apache Groovy's Metaprogramming Options and You
Andres Almiray
 
QwalKeko, a History Querying Tool
stevensreinout
 
Design patterns
Anas Alpure
 
JavaScript Beyond jQuery
Bobby Bryant
 
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
Codemotion
 
Ad

Recently uploaded (20)

PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Digital Circuits, important subject in CS
contactparinay1
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 

JavaScript OOPs

  • 2. OOP ● Inheritance ○ ○ ● An object is based on another object using the same implementation Code reuse Encapsulation ○ ○ ● Restricting access to some of the object's components Code extensibility Polymorphism ○ A single interface to entities of different types ○ Code flexibiity
  • 3. Classical vs Prototypal Inheritance ● Classical ○ ○ ● In most OOP languages, there are classes and objects Classes inherit from other classes Prototypal ○ JavaScript is prototypal based ○ An object inherits from another object
  • 4. Classical vs Prototypal Inheritance [[Object]] Animal [[CLASS]] Animal [[Object]] [[Object]] Cat Cat [[Object]] [[Object]] Dog Dog
  • 5. Peudoclassical ● Contructor Pattern ○ Using the new keyword ○ Using this to store object own status ○ Using prototype to share methods
  • 6. Pesudoclassical ● Instantiation 1 function Rectangle(width, height) { 2 this.height = height; 3 this.width = width; 4} 5 6 Rectangle.prototype.area = function () { 7 return this.width * this.height; 8 }; 9 10 var rect = new Rectangle(5, 10);
  • 7. Pesudoclassical ● Subclass Inheritance 1 function Square(side) { 2 Rectangle.call(this, side, side); 3} 4 5 Square.prototype = Object.create(Rectangle.prototype); 6 7 Square.prototype.constructor = Square; 8 9 var sq = new Square(5); 10 11 alert(sq.area());
  • 8. Pesudoclassical ● Encapsulation & Privileged Method 1 function Game() { 2 var name = ‘U3D’; 3 var version = 0.5; 4 this.getVersion = function () { 5 return ‘Version: ’ + name + ‘ - ’ + version; 6 } 7} 8 Game.prototype.play = function () { return ‘play’; }; 9 10 var g = new Game(); 11 alert(g.getVersion());
  • 9. Pesudoclassical ● 1 2 3 4 5 6 7 8 9 Polymorphism function SpaceGame() { Game.call(this); } ... SpaceGame.prototype.play = function () { return ‘space play’; }; var sg = new SpaceGame(); alert(sg.play());
  • 10. Comparison With Prototypal ● 1 2 3 4 5 6 7 8 Instantiation var rect = { height: 5, width: 10 } rect.area = function () { return this.width * this.height; }; 1 function Rectangle(width, height) { 2 this.height = height; 3 this.width = width; 4} 5 6 Rectangle.prototype.area = function () { 7 return this.width * this.height; 8 }; 9 10 var rect = new Rectangle(5, 10);
  • 11. Comparison With Prototypal ● 1 2 3 4 5 6 Subclass Inheritance var sq = Object.create(rect); sq.height = 5; sq.width = 5; alert(sq.area()); 1 function Square(side) { 2 Rectangle.call(this, side, side); 3} 4 5 Square.prototype = Object.create(Rectangle.prototype); 6 7 Square.prototype.constructor = Square; 8 9 var sq = new Square(5); 10 11 alert(sq.area());
  • 12. Comparison With Prototypal ● Encapsulation & Privileged Method 1 var g = {}; 2 // Imediate function & closure 3 g.getVersion = (function() { 4 var name = ‘U3D’; 5 var version = 0.5; 6 return function() { 7 return ‘Version: ’ + name + ‘ - ’ + version; 8 } 9 })(); 10 11 alert(g.getVersion());
  • 13. Comparison With Prototypal ● 1 2 3 4 5 6 7 Polymorphism var sg = Object.create(g); sg.play = function() { return ‘space play’; }; alert(sg.play()); 1 function SpaceGame() { 2 Game.call(this); 3} 4 ... 5 SpaceGame.prototype.play = function () { 6 return ‘space play’; 7 }; 8 9 var sg = new SpaceGame(); 10 11 alert(sg.play());
  • 14. Stop Using The new Keyword ● Raynos new is a remnant of the days where JavaScript accepted a Java like syntax for gaining “popularity” ● Brendan Eich And we were pushing it as a little brother to Java, as a complementary language like Visual Basic was to C++ in Microsoft’s language families at the time
  • 15. Stop Using The new Keyword ● Douglas Crockford This indirection was intended to make the language seem more familiar to classically trained programmers, but failed to do that, as we can see from the very low opinion Java programmers have of JavaScript. JavaScript’s constructor pattern did not appeal to the classical crowd. It also obscured JavaScript’s true prototypal nature. As a result, there are very few programmers who know how to use the language effectively.
  • 16. Advantages of Prototypal Pattern Constructor Pattern Prototypal Pattern Functional features can't be used in conjunction with the new keyword. Functional features can be used in conjunction with create. Forgetting to use new leads to unexpected bugs and global variables. Since create is a function the program will always work as expected. Prototypal inheritance is unnecessarily complicated and confusing. Prototypal inheritance is simple and easy to understand.
  • 17. Two Types of Prototype-based Language ● Delegation In prototype-based languages that use delegation, the language runtime is capable of dispatching the correct method or finding the right piece of data simply by following a series of delegation pointers (from object to its prototype) until a match is found. ● Concatenation … there are no visible pointers or links to the original prototype from which an object is cloned. The prototype (parent) object is copied rather than linked to. As a result, changes to the prototype will not be reflected in cloned objects.
  • 18. Delegation vs Concatenation Inheritance [[Object]] [[Object]] Animal Animal [[Object]] [[Object]] Cat Cat [[Object]] Dog [[Object]] Dog
  • 19. Pros and Cons Delegation Concatenation Any changes to the prototype are automatically reflected on all its clones. Any changes to the prototype need to be propagated to all its clones. Property access is slower because it may need to traverse up the prototype chain. Property access is faster because inherited properties are copied. Objects may only delegate to a single prototype in JavaScript. Objects may copy properties from any number of prototypes.
  • 20. Functional Pattern(Factory Pattern) ● Douglas Crockford The functional pattern has a great deal of flexibility. It requires less effort than the pseudoclassical pattern, and gives us better encapsulation and information hiding and access to super methods. ● GoF Favor object composition over class inheritance
  • 21. Functional Pattern ● Instantiation 1 function rectangle(spec) { 2 var that = {}; 3 that.height = spec.height; 4 that.width = spec.width; 5 that.area = function() { return spec.width * spec.height; }; 5 return that; 6} 7 8 var rect = rectangle({height: 5, width: 10});
  • 22. Functional Pattern ● 1 2 3 4 5 6 7 Subclass Inheritance function square(spec) { var that = rectangle({height: spec.side, width: spec.side}); return that; } var sq = square({side: 5}); alert(sq.area());
  • 23. Functional Pattern ● Encapsulation & Privileged Method 1 function square(spec) { 2 var that = rectangle({height: spec.side, width: spec.side}); 3 4 var varsion = ‘1.0’; 5 that.getVersion = function() { return ‘Version: ’ + version; }; 6 7 return that; 8} 9 10 var sq = square({side: 5}); 11 alert(sq.getVersion());
  • 24. Functional Pattern ● 1 2 3 4 5 6 7 8 Polymorphism function square(spec) { var that = rectangle({height: spec.side, width: spec.side}); that.area = function(){ return spec.width * spec.height * 5; } return that; } var sq = square({side: 5}); alert(sq.area());
  • 26. Reference ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● Benefits of prototypal inheritance over classical Why prototypal inheritance matters? Prototypal-based programming Prototypal Inheritance in JavaScript Stop Using Constructor Functions In JavaScript Fluent JavaScript - Three Different Kinds of Prototypal OO Classical Inheritance Is Obsolete - How To Think In Prototypal OO Mixin Pattern In JavaScript JavaScript Module Pattern: In-Depth Prototype-based Programming Delegation Programming Dynamic Dispatch What The Fuck Is Prototypal Inheritance? Introduction to Object-Oriented JavaScript Inheritance and Prototype Chain Prototypal Inheritance Comparing Prototypal and Classical Inheritance Classical vs Prototypal Inheritance JavaScript Prototypal Object Oriented Programming in Practice Make Object-Oriented Programming Easier With Only Six Lines Of JavaScript
  • 27. Reference ● ● ● ● ● ● ● ● ● ● JavaScript Constructor Prototypes and The new Keyword Create Advanced Web Applications With Object-Oriented Techniques Scripting Mantainability Optimizing JavaScript for Extreme Performance and Low Memory Consumption OO Design Patterns JavaScript Is Sexy JavaScript Inheritance Patterns JavaScript: The Good Parts Secrets of The JavaScript Ninja JavaScript: The Definitive Guide, 6th Edition