Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Understanding JavaScript Prototypes
Search
sporto
August 11, 2013
Programming
3
320
Understanding JavaScript Prototypes
Also see
http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/
sporto
August 11, 2013
Tweet
Share
More Decks by sporto
See All by sporto
React inside Elm
sporto
2
170
Elm
sporto
1
250
Redux: Flux Reduced
sporto
1
320
Practically Immutable
sporto
0
170
Webpack and React
sporto
4
380
Rails with Webpack
sporto
1
210
Lesson learnt building Single Page Application
sporto
0
110
Grunt
sporto
1
150
Safe Testing in Ruby
sporto
1
120
Other Decks in Programming
See All in Programming
Patched fetch did not work
quramy
4
400
Ebitengineの1vs1ゲーム WebRTCの活用
ponyo877
0
380
The Sequel to a Dream of Ruby Parser's Grammar
ydah
1
220
僕が思い描くTypeScriptの未来を勝手に先取りする
yukukotani
9
2.4k
Rubyのobject_id
qnighy
6
1.3k
意外とフォントが大事だった話 / Font Issues on Internationalization
fumi23
0
110
2024 컴포즈 정원사
jisungbin
0
150
サーバーレスで負荷試験!Step Functions + Lambdaを使ったk6の分散実行
shuntakahashi
6
1.6k
Jakarta EE meets AI
ivargrimstad
1
540
Some more adventure of Happy Eyeballs
coe401_
2
190
はじめてみよう量子プログラミング
itokoichi01
0
220
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
0
120
Featured
See All Featured
Infographics Made Easy
chrislema
239
18k
Clear Off the Table
cherdarchuk
91
320k
What’s in a name? Adding method to the madness
productmarketing
PRO
21
3k
[RailsConf 2023] Rails as a piece of cake
palkan
48
4.6k
Embracing the Ebb and Flow
colly
83
4.4k
Six Lessons from altMBA
skipperchong
26
3.4k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
A Tale of Four Properties
chriscoyier
155
22k
BBQ
matthewcrist
83
9.2k
VelocityConf: Rendering Performance Case Studies
addyosmani
322
23k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
24
610
Web development in the modern age
philhawksworth
205
10k
Transcript
Understanding JS prototypes Sebastian Porto
+ Prototypes ! + Object.create ! + Constructor functions !
+ ES6
The wrong way to learn function Liquid() { } Liquid.prototype.fluid
= true; ! function Beer() { Liquid.call(this); } ! Beer.prototype = Object.create(Liquid.prototype); Beer.prototype.alcohol = 5.1; ! var beer = new Beer();
__proto__
• var liquid = {} var beer = {} •
beer.__proto__ = liquid A simple chain
A simple chain liquid beer __proto__ liquid is the prototype
of beer
Property look-ups • var liquid = { fluid: true }
• var beer = {} beer.__proto__ = liquid • beer.fluid //-> true
A simple chain liquid beer __proto__ fluid fluid? fluid?
A longer chain liquid coopers fluid fluid? beer drink
Long chain example • var liquid = { fluid: true
} • var drink = {} drink.__proto__ = liquid • var beer = {} beer.__proto__ = drink • var coopers = {} coopers.__proto__ = beer • coopers.fluid; //-> true
Many object can have same prototype liquid beer __proto__ cider
__proto__
Prototype can be shared • var liquid = { fluid:
true } • var beer = {} beer.__proto__ = liquid • var cider = {} cider.__proto__ = liquid • beer.fluid; //-> true cider.fluid; //-> true
Prototypes are not copies • var beer = { tasty:
true }; • var coopers = {}; coopers.__proto__ = beer; • coopers.tasty; //-> true • beer.tasty = false; • cooper.tasty; //-> false
beer coopers ethanol Prototypes are dynamic .__proto__
Prototypes are dynamic • var beer = { alcohol: 6
} var ethanol = { alcohol: 100 } • var coopers = {} coopers.__proto__ = beer • coopers.alcohol //-> 6 • coopers.__proto__ = ethanol • coopers.alcohol //-> 100
Writing creates properties • var beer = { alcohol: 6
} • var coopers = {} coopers.__proto__ = beer • coopers.alcohol = 4 • beer.alcohol //-> still 6
Creates ‘own’ property beer coopers alcohol = 6 .alcohol =
4 alcohol = 4 “own” property
beer coopers alcohol = 6 alcohol = 4 creatures Same
property can be in several places
Arrays • var beer = { ingredients: [‘wheat’, ‘yeast’]
} • var coopers = {} coopers.__proto__ = beer • coopers.ingredients.push(‘oats’) • beer.ingredients //->[‘wheat’, ‘yeast’, ‘oats’];
Direct modification beer coopers ingredients .ingredients.push(‘oats’)
coopers.ingredients.push(‘oats’); ! creatures.ingredients; //-> [‘wheat’, ‘yeast’, ‘oats’]; Arrays are modified
directly in the prototype beer coopers creatures ingredients ['wheat', 'yeast'] __proto__ __proto__
Objects are the same • var beer = {
ingredients: {wheat: 10, yeast: 20} } • var coopers = {} coopers.__proto__ = beer • coopers.ingredients.wheat = 30 • beer.ingredients.wheat //-> 30
__proto__ == great for learning ! Not for use
Object.create
Object.create • var beer = { tasty: true }
• var coopers = Object.create(beer) • coopers.tasty //-> true
Object.create var coopers = Object.create(beer); ! Just like: ! var
coopers = {}; coopers.__proto__ = beer;
Object.create is just setting __proto__ ! so same rules apply
- not copies - dynamic
getPrototypeOf var beer = { tasty: true }; ! var
coopers = Object.create(beer); ! ! Object.getPrototypeOf(coopers) //-> beer object
isPrototypeOf var beer = { tasty: true }; ! var
coopers = Object.create(beer); ! ! beer.isPrototypeOf(coopers); //-> true
ES6 setPrototypeOf var beer = { tasty: true }; !
var coopers = {}; ! ! Object.setPrototypeOf(coopers, beer);
Constructor Functions
Functions as constructors function Beer(){ ! } ! var beer
= new Beer(); Uppercase by convention
function Beer(){ ! ! ! ! ! } ! var
beer = new Beer(); Implicit this ! var this = {} this.__proto__ = Beer.prototype (yield to your code) return this
Implicit this function Beer(){ var this = {}; this.__proto__ =
Beer.prototype; this.tasty = true; return this; } ! var beer = new Beer(); ! beer.tasty; // true
function Beer(){ this.tasty = true; } ! var beer =
new Beer(); ! beer.tasty; //-> true Implicit this
Don’t forget new function Beer(){ this.kind = ‘beer’; } !
var beer = Beer(); this will the global object
function Beer(){ ! this.__proto__ = Beer.prototype; ! } function.prototype
function.prototype function Beer(){ ! } ! Beer.prototype Every function has
this special property
But this is not the prototype Beer.prototype !== Beer.__proto__ Call
this ‘function prototype’
function.prototype function Beer(){ ! } ! var beer = new
Beer(); beer.__proto__ ???? What is the __proto__ of beer?
It is assigned as the prototype of the instance function
Beer(){ var this = {}; this.__proto__ = Beer.prototype; this.tasty = true; return this; } It is assigned as the prototype of the new created object
Function instance new __proto__ Function prototype .prototype
• function Beer(){} • Beer.prototype.tasty = true • var coopers
= new Beer() • coopers.__proto__ == Beer.prototype //-> true • coopers.tasty //-> true It is the prototype assigned to the instance
function Beer(){ } ! Beer.prototype.brew = function () {} !
var coopers = new Beer(); var creatures = new Beer(); ! coopers.brew(); creatures.brew(); ! Useful for performance
• function Beer(){...} • Beer.prototype.ingredients = ['honey'] • var coopers
= new Beer() var vb = new Beer() • coopers.ingredients.push('oats') • vb.ingredients //-> ['honey', 'oats'] Again, same rules!
• function Beer(){ this.ingredients = [‘yeast’] } • var coopers
= new Beer() var creatures = new Beer() • coopers.ingredients.push(‘wheat’) • creatures.ingredients //-> [‘yeast’] Isolation
function Beer(){ ! } ! var coopers = new Beer();
! coopers instanceof Beer; //-> true ! instanceof
coopers instanceof Beer; ! ! Checks that ! coopers.__proto__ ==
Beer.prototype ! instanceof
var Beer = function () { ! }; ! Beer.findAll
= function () { ... } ! Beer.findAll(); "Class methods"
ES6
class Beer { constructor(a) { this.alcohol = a; } !
drink() { ... } } ES6 Classes
ES6 Classes function Beer(a){ this.alcohol = a; } ! Beer.prototype.drink
= function () { ... } ! var beer = new Beer(5) class Beer { constructor(a) { this.alcohol = a; } ! drink() { ... } } ! var beer = new Beer(5)
Questions? Thanks