SlideShare a Scribd company logo
1 of 27
Download to read offline
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

JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - ObjectsWebStackAcademy
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testingVikas Thange
 
Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6Devang Garach
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)raja kvk
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJWORKS powered by Ordina
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesDoris Chen
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101ygv2000
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayChamnap Chhorn
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...Edureka!
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptWalid Ashraf
 
JavaScript - From Birth To Closure
JavaScript - From Birth To ClosureJavaScript - From Birth To Closure
JavaScript - From Birth To ClosureRobert Nyman
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An IntroductionManvendra Singh
 

What's hot (20)

Lecture 5 javascript
Lecture 5 javascriptLecture 5 javascript
Lecture 5 javascript
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testing
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
JavaScript - From Birth To Closure
JavaScript - From Birth To ClosureJavaScript - From Birth To Closure
JavaScript - From Birth To Closure
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 

Viewers also liked

Prototypal inheritance in JavaScript
Prototypal inheritance in JavaScriptPrototypal inheritance in JavaScript
Prototypal inheritance in JavaScriptMiroslav Obradović
 
Listening barriers
Listening barriersListening barriers
Listening barriersmbrownpartin
 
UKLUG 2012 Leverage user adoption with gamification
UKLUG 2012 Leverage user adoption with gamificationUKLUG 2012 Leverage user adoption with gamification
UKLUG 2012 Leverage user adoption with gamificationSasja Beerendonk
 
Deep diveconnections eco systeem
Deep diveconnections eco systeemDeep diveconnections eco systeem
Deep diveconnections eco systeemSasja Beerendonk
 
BP305 Show me the money! The value in social business
BP305 Show me the money! The value in social businessBP305 Show me the money! The value in social business
BP305 Show me the money! The value in social businessSasja Beerendonk
 
Connect, it WorX for secretaries
Connect, it WorX for secretariesConnect, it WorX for secretaries
Connect, it WorX for secretariesSasja Beerendonk
 
Internet Week Yahoo! Academy: Working the Digital Canvas
Internet Week Yahoo! Academy: Working the Digital CanvasInternet Week Yahoo! Academy: Working the Digital Canvas
Internet Week Yahoo! Academy: Working the Digital CanvasYahooUK
 
Introduction to omap4 pad configuration
Introduction to omap4 pad configurationIntroduction to omap4 pad configuration
Introduction to omap4 pad configurationJohnson Chou
 
[Webinar] Office 365:Slimmer samenwerken in een project
[Webinar] Office 365:Slimmer samenwerken in een project[Webinar] Office 365:Slimmer samenwerken in een project
[Webinar] Office 365:Slimmer samenwerken in een projectSasja Beerendonk
 
User Adoption Strategies - 2 days workshops @Silverside
User Adoption  Strategies - 2 days workshops @SilversideUser Adoption  Strategies - 2 days workshops @Silverside
User Adoption Strategies - 2 days workshops @SilversideSasja Beerendonk
 
CSS Sanity with Sass: The Inverted Triangle Approach
CSS Sanity with Sass: The Inverted Triangle ApproachCSS Sanity with Sass: The Inverted Triangle Approach
CSS Sanity with Sass: The Inverted Triangle ApproachJulie Kuehl
 
Advanced sass/compass
Advanced sass/compassAdvanced sass/compass
Advanced sass/compassNick Cooley
 
Change behavior, one Tiny Habit at a time
Change behavior, one Tiny Habit at a timeChange behavior, one Tiny Habit at a time
Change behavior, one Tiny Habit at a timeSasja Beerendonk
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized军 沈
 
[Webinar] Office 365: Slimmer vergaderen
[Webinar] Office 365: Slimmer vergaderen[Webinar] Office 365: Slimmer vergaderen
[Webinar] Office 365: Slimmer vergaderenSasja Beerendonk
 
Smart CSS theming with Sass and Compass
Smart CSS theming with Sass and CompassSmart CSS theming with Sass and Compass
Smart CSS theming with Sass and CompassMihaela
 
Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)Tahmina Khatoon
 

Viewers also liked (20)

Prototypal inheritance in JavaScript
Prototypal inheritance in JavaScriptPrototypal inheritance in JavaScript
Prototypal inheritance in JavaScript
 
Listening barriers
Listening barriersListening barriers
Listening barriers
 
Retooling of Social Studies
Retooling of Social StudiesRetooling of Social Studies
Retooling of Social Studies
 
UKLUG 2012 Leverage user adoption with gamification
UKLUG 2012 Leverage user adoption with gamificationUKLUG 2012 Leverage user adoption with gamification
UKLUG 2012 Leverage user adoption with gamification
 
Deep diveconnections eco systeem
Deep diveconnections eco systeemDeep diveconnections eco systeem
Deep diveconnections eco systeem
 
BP305 Show me the money! The value in social business
BP305 Show me the money! The value in social businessBP305 Show me the money! The value in social business
BP305 Show me the money! The value in social business
 
Connect, it WorX for secretaries
Connect, it WorX for secretariesConnect, it WorX for secretaries
Connect, it WorX for secretaries
 
Internet Week Yahoo! Academy: Working the Digital Canvas
Internet Week Yahoo! Academy: Working the Digital CanvasInternet Week Yahoo! Academy: Working the Digital Canvas
Internet Week Yahoo! Academy: Working the Digital Canvas
 
Introduction to omap4 pad configuration
Introduction to omap4 pad configurationIntroduction to omap4 pad configuration
Introduction to omap4 pad configuration
 
[Webinar] Office 365:Slimmer samenwerken in een project
[Webinar] Office 365:Slimmer samenwerken in een project[Webinar] Office 365:Slimmer samenwerken in een project
[Webinar] Office 365:Slimmer samenwerken in een project
 
User Adoption Strategies - 2 days workshops @Silverside
User Adoption  Strategies - 2 days workshops @SilversideUser Adoption  Strategies - 2 days workshops @Silverside
User Adoption Strategies - 2 days workshops @Silverside
 
CSS Sanity with Sass: The Inverted Triangle Approach
CSS Sanity with Sass: The Inverted Triangle ApproachCSS Sanity with Sass: The Inverted Triangle Approach
CSS Sanity with Sass: The Inverted Triangle Approach
 
Prototype
PrototypePrototype
Prototype
 
SASS - CSS with Superpower
SASS - CSS with SuperpowerSASS - CSS with Superpower
SASS - CSS with Superpower
 
Advanced sass/compass
Advanced sass/compassAdvanced sass/compass
Advanced sass/compass
 
Change behavior, one Tiny Habit at a time
Change behavior, one Tiny Habit at a timeChange behavior, one Tiny Habit at a time
Change behavior, one Tiny Habit at a time
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
[Webinar] Office 365: Slimmer vergaderen
[Webinar] Office 365: Slimmer vergaderen[Webinar] Office 365: Slimmer vergaderen
[Webinar] Office 365: Slimmer vergaderen
 
Smart CSS theming with Sass and Compass
Smart CSS theming with Sass and CompassSmart CSS theming with Sass and Compass
Smart CSS theming with Sass and Compass
 
Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)
 

Similar to JavaScript OOPs

Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
Java design patterns
Java design patternsJava design patterns
Java design patternsShawn Brito
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)Piyush Katariya
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 
Modeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based GamesModeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based GamesRay Toal
 
Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaDILo Surabaya
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneAndres Almiray
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityGeorgePeterBanyard
 
Douglas Crockford: Serversideness
Douglas Crockford: ServersidenessDouglas Crockford: Serversideness
Douglas Crockford: ServersidenessWebExpo
 
Apache Groovy's Metaprogramming Options and You
Apache Groovy's Metaprogramming Options and YouApache Groovy's Metaprogramming Options and You
Apache Groovy's Metaprogramming Options and YouAndres Almiray
 
QwalKeko, a History Querying Tool
QwalKeko, a History Querying ToolQwalKeko, a History Querying Tool
QwalKeko, a History Querying Toolstevensreinout
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQueryBobby Bryant
 
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...Codemotion
 

Similar to JavaScript OOPs (20)

Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
Java Script Workshop
Java Script WorkshopJava Script Workshop
Java Script Workshop
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
Modeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based GamesModeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based Games
 
Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo Surabaya
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast Lane
 
Java
JavaJava
Java
 
Clojure And Swing
Clojure And SwingClojure And Swing
Clojure And Swing
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
 
Douglas Crockford: Serversideness
Douglas Crockford: ServersidenessDouglas Crockford: Serversideness
Douglas Crockford: Serversideness
 
oojs
oojsoojs
oojs
 
What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
Apache Groovy's Metaprogramming Options and You
Apache Groovy's Metaprogramming Options and YouApache Groovy's Metaprogramming Options and You
Apache Groovy's Metaprogramming Options and You
 
QwalKeko, a History Querying Tool
QwalKeko, a History Querying ToolQwalKeko, a History Querying Tool
QwalKeko, a History Querying Tool
 
Design patterns
Design patternsDesign patterns
Design patterns
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
 
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
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 TerraformAndrey Devyatkin
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
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.pdfsudhanshuwaghmare1
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
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 Takeoffsammart93
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

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