SlideShare ist ein Scribd-Unternehmen logo
1 von 16
JAVASCRIPT PROTOTYPE
VISUALIZED
come and bite me if you still don’t understand the concept of prototype after
this presentation
PROTOTYPE OF FUNCTION
Every function is born with a prototype object, it’s used as the shared
prototype(parent) of the objects created by this function( invoked as constructor
function).
The prototype is initially an empty object, you can add members to it. Such all its
“children” have access to these members( properties, methods ) as well.
F
prototyp
e
constructo
r
F.prototype
F.prototype.constructor
PROTOTYPE OF OBJECT
Every Object is born referencing to a prototype object(parent) by a secret property
__proto__.
__proto__
WHAT’S THE RELATIONSHIP?
F
prototyp
e
constructo
r
__proto__
What’s the relationship between these two
concept?
?
var Person = function(name, age){
this.name = name;
this.age = age;
};
Person.prototype.sayMyName = function(){
console.log("I'm " + this.name);
};
var linus = new Person('Linus Torvalds');
linus.sayMyName();
CONSTRUCT A NEW OBJECT
Person
prototyp
e
constructo
r
__proto__
linus.sayMyName
sayMyNamename: Linus
Torvaldsage: 46
name: Alice
age: 17
name: Bob
age: 18
new
let’s walk through the following simple
code to understand the whole process
there’re 3 steps are done by the
javascript engine whenever a new
instance is created, let’s see them in
action:1. create a bare object
2. create a link “__proto__” points to the
prototype of the constructor function
3. execute the function body of the
constructor function
PROTOTYPE CHAIN
Object
prototyp
e
constructo
r
F
prototyp
e
constructo
r
__proto____proto__
ne
w
built-
in
This is the prototype
chain
__proto__
Object.prototype is the top of prototype
chain, Object.prototype doesn’t have
prototype
OBJECT LITERAL
Object
prototyp
e
constructo
r
__proto__
ne
w
anObjec
t
var anObject = {};
// is equal to
var anObject = new Object();
built-
in
RESOLVE PROPERTY
var value = anObject.someProperty;
Object
prototyp
e
constructo
r
F1
prototyp
e
constructo
r
__proto____proto__
built-
in
F2
prototyp
e
constructo
r
__proto__
anObject
Lookup “someProperty” on
anObject
1
2
b
not found, continue
lookup “someProperty” on
its __proto__ object
found, return
value
2a
found, return
value
3a
3
b
not found, continue
lookup “someProperty” on
its __proto__ object
found, return
value
4a
found, return
value
5a
4
b
not found, continue
lookup “someProperty” on
its __proto__ object
5
b
not found, return
undefined
let’s see how javascript engine resolve
property lookup
MODIFY PROPERTY
anObject.someProperty = “some property”;
Object
prototyp
e
constructo
r
F1
prototyp
e
constructo
r
__proto____proto__
built-
in
F2
prototyp
e
constructo
r
__proto__
somePropert
y
somePropert
y
anObject
When you do changes to a property of
an object, it always affect the current
object only. If the property doesn’t
exist, property is added to the object. It
won’t look up to the prototype chain.
PSEUDO-CLASSICAL INHERITANCE
Develope
r
prototyp
e
constructo
r
__proto__
Person
prototyp
e
constructo
r
F
prototyp
e
new
prototyp
e
constructo
r
prototyp
e
constructo
r
I put the prototype and function up
down just to leave room for following
animation, don’t be confused here, it’s
the same stuff
let’s walk through the following code to see how
classical inheritance is achieved
let’s remove the irrelevant part now
Now we’ve achieved pseudo-classical
inheritance !!!
WRONG PSEUDO-CLASSICAL
INHERITANCE
Develope
r
prototyp
e
constructo
r
__proto__
Person
prototyp
e
constructo
r
new
Develope
r
prototyp
e
constructo
r
__proto__
Person
prototyp
e
constructo
r
F
prototyp
e
new
wrong
correc
t
let’s look at a frequent mistake of doing classical
inheritance
Why is the first example is wrong ? Well, I
wouldn’t say it’s always wrong, but in
most cases, it’s wrong. Because the
subclass Developer’s prototype is an
instance of Person Class, that means it’s a
special individual person. And in most
case, the Person Constructor would
require some arguments to initialize a
Person instance, such as: name, age … Do
we want these properties on the prototype
of Developer ? No ! What we want is a bare
object which just has a “__proto__” points
to the prototype of Person Class. That’s
exactly how the second example does.
Through a temporary constructor function
F which does nothing in its constructor, it
will create a bare object points to the
prototype of F which is equal to prototype
OBJECT.CREATE
In ES5, a method is included to implement inheritance:
// utility function for inheritance
var inherit = function(proto){
function F(){}
F.prototype = proto;
return new F();
};
our simple version
Object.create
ES5 version, more
powerful
PROTOTYPAL INHERITANCE
Develope
r
Person
__proto__
let’s walk through the following code to see how
prototypal inheritance is achieved
linus
__proto__
That’s it, we accomplished prototypal inheritance. You
can see how much easier prototypal inheritance is than
Classical inheritance. That’s because it completely
discard Constructor parts. And more importantly, in
javascript, the essence of inheritance is through the
“__proto__” link between objects, aka. prototype chain.
At the heart of the classical inheritance, it’s also using
the prototype chain achieving inheritance. Only it add
an extra layer of “Constructor” to simulate the “Class”
concept from other language: java, c#... to make it more
comfortable for developers from those languages. Even
it feels somehow comfortable at first, but without truly
understanding of the essence of prototypal inheritance,
You’ll get lost and confused at last !!! So that’s why a lot
of javascript gurus advocate prototypal inheritance and
recommend avoid using of classical inheritance.
__PROTO__ OF FUNCTION
Every function is a “child” of Function.prototype
F
prototyp
e
constructo
r
__proto__
Function
prototyp
e
constructo
r
function
Empty(){}
Object
prototyp
e
constructo
r
__proto__
__proto__
__proto__
ne
w
__proto__
built-
in
built-
in
JAVASCRIPT OBJECT LAYOUT
This is a graph of object layout I grab it
from some online website. If you
understand the prototype concept
correctly, you can easily understand the
graph. But personally, I feel it’s a little
messy with all the arrows floating around.
Anyway, it’s a great graph for your
reference when you forget the
relationship.
THE END
Thank you for watching!
Author: Jun Shen
I’m an enthusiast about javascript
Javascript Rocks!!!
linked in: https://www.linkedin.com/in/jun-shen-55b01959

Weitere ähnliche Inhalte

Was ist angesagt?

javascript objects
javascript objectsjavascript objects
javascript objectsVijay Kalyan
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - ObjectsWebStackAcademy
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentationritika1
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVCIndicThreads
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Aaron Gustafson
 
Java Collections
Java CollectionsJava Collections
Java Collectionsparag
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
Hibernate
HibernateHibernate
HibernateAjay K
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners Varun Raj
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...Edureka!
 
AngularJS for Beginners
AngularJS for BeginnersAngularJS for Beginners
AngularJS for BeginnersEdureka!
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksSamundra khatri
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in phpCPD INDIA
 

Was ist angesagt? (20)

javascript objects
javascript objectsjavascript objects
javascript objects
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVC
 
jQuery
jQueryjQuery
jQuery
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Hibernate
HibernateHibernate
Hibernate
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
Javascript
JavascriptJavascript
Javascript
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
 
AngularJS for Beginners
AngularJS for BeginnersAngularJS for Beginners
AngularJS for Beginners
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 

Andere mochten auch

JavaScript Prototype and Module Pattern
JavaScript Prototype and Module PatternJavaScript Prototype and Module Pattern
JavaScript Prototype and Module PatternNarendra Sisodiya
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptzand3rs
 
Prototype-based Programming with JavaScript
Prototype-based Programming with JavaScriptPrototype-based Programming with JavaScript
Prototype-based Programming with JavaScriptlienhard
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptForziatech
 
Prototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScriptPrototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScriptSunny Sharma
 
Oo java script class construction
Oo java script class constructionOo java script class construction
Oo java script class constructionKen Collins
 
03. Introduccion a JavaScript y JQuery
03. Introduccion a JavaScript y JQuery03. Introduccion a JavaScript y JQuery
03. Introduccion a JavaScript y JQueryDanae Aguilar Guzmán
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetHDR1001
 
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
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayChamnap Chhorn
 
JavaScript: The prototype Property
JavaScript: The prototype PropertyJavaScript: The prototype Property
JavaScript: The prototype PropertyGuillermo Paz
 
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
 
Advanced sass/compass
Advanced sass/compassAdvanced sass/compass
Advanced sass/compassNick Cooley
 
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
 
Advanced JavaScript Concepts
Advanced JavaScript ConceptsAdvanced JavaScript Concepts
Advanced JavaScript ConceptsNaresh Kumar
 
Sass - Getting Started with Sass!
Sass - Getting Started with Sass!Sass - Getting Started with Sass!
Sass - Getting Started with Sass!Eric Sembrat
 

Andere mochten auch (20)

Prototype
PrototypePrototype
Prototype
 
JavaScript Prototype and Module Pattern
JavaScript Prototype and Module PatternJavaScript Prototype and Module Pattern
JavaScript Prototype and Module Pattern
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
Prototype-based Programming with JavaScript
Prototype-based Programming with JavaScriptPrototype-based Programming with JavaScript
Prototype-based Programming with JavaScript
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
Prototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScriptPrototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScript
 
Oo java script class construction
Oo java script class constructionOo java script class construction
Oo java script class construction
 
03. Introduccion a JavaScript y JQuery
03. Introduccion a JavaScript y JQuery03. Introduccion a JavaScript y JQuery
03. Introduccion a JavaScript y JQuery
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat Sheet
 
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
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
 
JavaScript: The prototype Property
JavaScript: The prototype PropertyJavaScript: The prototype Property
JavaScript: The prototype Property
 
SASS - CSS with Superpower
SASS - CSS with SuperpowerSASS - CSS with Superpower
SASS - CSS with Superpower
 
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)
 
Advanced sass/compass
Advanced sass/compassAdvanced sass/compass
Advanced sass/compass
 
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)
 
JavaScript OOPs
JavaScript OOPsJavaScript OOPs
JavaScript OOPs
 
Advanced JavaScript Concepts
Advanced JavaScript ConceptsAdvanced JavaScript Concepts
Advanced JavaScript Concepts
 
Sass - Getting Started with Sass!
Sass - Getting Started with Sass!Sass - Getting Started with Sass!
Sass - Getting Started with Sass!
 

Ähnlich wie Javascript Prototype Visualized

Inverting Dependencies
Inverting DependenciesInverting Dependencies
Inverting DependenciesLuc Trudeau
 
How prototype works in java script?
How prototype works in java script?How prototype works in java script?
How prototype works in java script?InnovationM
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep DiveManish Jangir
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 
Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02plutoone TestTwo
 
The prototype property
The prototype propertyThe prototype property
The prototype propertyHernan Mammana
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternNishith Shukla
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsLalit Kale
 
Javascript Object Oriented Programming
Javascript Object Oriented ProgrammingJavascript Object Oriented Programming
Javascript Object Oriented ProgrammingBunlong Van
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascriptUsman Mehmood
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAyush Sharma
 

Ähnlich wie Javascript Prototype Visualized (20)

Inverting Dependencies
Inverting DependenciesInverting Dependencies
Inverting Dependencies
 
How prototype works in java script?
How prototype works in java script?How prototype works in java script?
How prototype works in java script?
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
 
Java script unleashed
Java script unleashedJava script unleashed
Java script unleashed
 
JavaScript Essentials
JavaScript EssentialsJavaScript Essentials
JavaScript Essentials
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Monad Fact #6
Monad Fact #6Monad Fact #6
Monad Fact #6
 
Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02
 
The prototype property
The prototype propertyThe prototype property
The prototype property
 
All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
Javascript talk
Javascript talkJavascript talk
Javascript talk
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
 
JavaScript, qué hermoso eres
JavaScript, qué hermoso eresJavaScript, qué hermoso eres
JavaScript, qué hermoso eres
 
All of javascript
All of javascriptAll of javascript
All of javascript
 
Ch2
Ch2Ch2
Ch2
 
Javascript Object Oriented Programming
Javascript Object Oriented ProgrammingJavascript Object Oriented Programming
Javascript Object Oriented Programming
 
Object
ObjectObject
Object
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 

Kürzlich hochgeladen

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
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 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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
🐬 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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Kürzlich hochgeladen (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
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 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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Javascript Prototype Visualized

  • 1. JAVASCRIPT PROTOTYPE VISUALIZED come and bite me if you still don’t understand the concept of prototype after this presentation
  • 2. PROTOTYPE OF FUNCTION Every function is born with a prototype object, it’s used as the shared prototype(parent) of the objects created by this function( invoked as constructor function). The prototype is initially an empty object, you can add members to it. Such all its “children” have access to these members( properties, methods ) as well. F prototyp e constructo r F.prototype F.prototype.constructor
  • 3. PROTOTYPE OF OBJECT Every Object is born referencing to a prototype object(parent) by a secret property __proto__. __proto__
  • 4. WHAT’S THE RELATIONSHIP? F prototyp e constructo r __proto__ What’s the relationship between these two concept? ?
  • 5. var Person = function(name, age){ this.name = name; this.age = age; }; Person.prototype.sayMyName = function(){ console.log("I'm " + this.name); }; var linus = new Person('Linus Torvalds'); linus.sayMyName(); CONSTRUCT A NEW OBJECT Person prototyp e constructo r __proto__ linus.sayMyName sayMyNamename: Linus Torvaldsage: 46 name: Alice age: 17 name: Bob age: 18 new let’s walk through the following simple code to understand the whole process there’re 3 steps are done by the javascript engine whenever a new instance is created, let’s see them in action:1. create a bare object 2. create a link “__proto__” points to the prototype of the constructor function 3. execute the function body of the constructor function
  • 6. PROTOTYPE CHAIN Object prototyp e constructo r F prototyp e constructo r __proto____proto__ ne w built- in This is the prototype chain __proto__ Object.prototype is the top of prototype chain, Object.prototype doesn’t have prototype
  • 7. OBJECT LITERAL Object prototyp e constructo r __proto__ ne w anObjec t var anObject = {}; // is equal to var anObject = new Object(); built- in
  • 8. RESOLVE PROPERTY var value = anObject.someProperty; Object prototyp e constructo r F1 prototyp e constructo r __proto____proto__ built- in F2 prototyp e constructo r __proto__ anObject Lookup “someProperty” on anObject 1 2 b not found, continue lookup “someProperty” on its __proto__ object found, return value 2a found, return value 3a 3 b not found, continue lookup “someProperty” on its __proto__ object found, return value 4a found, return value 5a 4 b not found, continue lookup “someProperty” on its __proto__ object 5 b not found, return undefined let’s see how javascript engine resolve property lookup
  • 9. MODIFY PROPERTY anObject.someProperty = “some property”; Object prototyp e constructo r F1 prototyp e constructo r __proto____proto__ built- in F2 prototyp e constructo r __proto__ somePropert y somePropert y anObject When you do changes to a property of an object, it always affect the current object only. If the property doesn’t exist, property is added to the object. It won’t look up to the prototype chain.
  • 10. PSEUDO-CLASSICAL INHERITANCE Develope r prototyp e constructo r __proto__ Person prototyp e constructo r F prototyp e new prototyp e constructo r prototyp e constructo r I put the prototype and function up down just to leave room for following animation, don’t be confused here, it’s the same stuff let’s walk through the following code to see how classical inheritance is achieved let’s remove the irrelevant part now Now we’ve achieved pseudo-classical inheritance !!!
  • 11. WRONG PSEUDO-CLASSICAL INHERITANCE Develope r prototyp e constructo r __proto__ Person prototyp e constructo r new Develope r prototyp e constructo r __proto__ Person prototyp e constructo r F prototyp e new wrong correc t let’s look at a frequent mistake of doing classical inheritance Why is the first example is wrong ? Well, I wouldn’t say it’s always wrong, but in most cases, it’s wrong. Because the subclass Developer’s prototype is an instance of Person Class, that means it’s a special individual person. And in most case, the Person Constructor would require some arguments to initialize a Person instance, such as: name, age … Do we want these properties on the prototype of Developer ? No ! What we want is a bare object which just has a “__proto__” points to the prototype of Person Class. That’s exactly how the second example does. Through a temporary constructor function F which does nothing in its constructor, it will create a bare object points to the prototype of F which is equal to prototype
  • 12. OBJECT.CREATE In ES5, a method is included to implement inheritance: // utility function for inheritance var inherit = function(proto){ function F(){} F.prototype = proto; return new F(); }; our simple version Object.create ES5 version, more powerful
  • 13. PROTOTYPAL INHERITANCE Develope r Person __proto__ let’s walk through the following code to see how prototypal inheritance is achieved linus __proto__ That’s it, we accomplished prototypal inheritance. You can see how much easier prototypal inheritance is than Classical inheritance. That’s because it completely discard Constructor parts. And more importantly, in javascript, the essence of inheritance is through the “__proto__” link between objects, aka. prototype chain. At the heart of the classical inheritance, it’s also using the prototype chain achieving inheritance. Only it add an extra layer of “Constructor” to simulate the “Class” concept from other language: java, c#... to make it more comfortable for developers from those languages. Even it feels somehow comfortable at first, but without truly understanding of the essence of prototypal inheritance, You’ll get lost and confused at last !!! So that’s why a lot of javascript gurus advocate prototypal inheritance and recommend avoid using of classical inheritance.
  • 14. __PROTO__ OF FUNCTION Every function is a “child” of Function.prototype F prototyp e constructo r __proto__ Function prototyp e constructo r function Empty(){} Object prototyp e constructo r __proto__ __proto__ __proto__ ne w __proto__ built- in built- in
  • 15. JAVASCRIPT OBJECT LAYOUT This is a graph of object layout I grab it from some online website. If you understand the prototype concept correctly, you can easily understand the graph. But personally, I feel it’s a little messy with all the arrows floating around. Anyway, it’s a great graph for your reference when you forget the relationship.
  • 16. THE END Thank you for watching! Author: Jun Shen I’m an enthusiast about javascript Javascript Rocks!!! linked in: https://www.linkedin.com/in/jun-shen-55b01959

Hinweis der Redaktion

  1. You can think of the prototype object is equal to super class at some degree
  2. var Person = function(name, age){ this.name = name; this.age = age; }; Person.prototype.selfIntroduce = function(){ return "I'm " + this.name + ", " + this.age + " years old."; }; // utility function for inheritance var inherit = function(proto){ function F(){} F.prototype = proto; return new F(); }; var Developer = function(name, age, language){ Person.call(this, name, age); this.language = language; }; // Developer's prototype to inherite from Person's prototype Developer.prototype = inherit(Person.prototype); Developer.prototype.constructor = Developer; var jun = new Developer('sj', 30, 'javascript'); jun.selfIntroduce();
  3. Don't create new Animal to inherit it There is a well-known, but wrong way of inhereting, when instead of Rabbit.prototype = inherit(Animal.prototype) people use: // inherit from Animal Rabbit.prototype = new Animal() As a result, we get a new Animal object in prototype. Inheritance works here, becausenew Animal naturally inherits Animal.prototype. … But who said that new Animal() can be called like without the name? The constructor may strictly require arguments and die without them. Actually, the problem is more conceptual than that. We don’t want to create an Animal. We just want to inherit from it. That’s why Rabbit.prototype = inherit(Animal.prototype) is preferred. The neat inheritance without side-effects.
  4. var person = { selfIntroduce: function(){ return "I'm " + this.name + ", " + this.age + " years old."; } }; // prototypal inherite from person var developer = Object.create(person); // create an instance of developer var jun = Object.create(developer); jun.name = 'jun'; jun.age = 30; jun.selfIntroduce();