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
330
Practically Immutable
sporto
0
180
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
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
250
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
CSC509 Lecture 13
javiergs
PRO
0
110
Remix on Hono on Cloudflare Workers
yusukebe
1
300
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
1k
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
940
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
150
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.3k
CSC509 Lecture 12
javiergs
PRO
0
160
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Raft: Consensus for Rubyists
vanstee
136
6.6k
How GitHub (no longer) Works
holman
310
140k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Into the Great Unknown - MozCon
thekraken
32
1.5k
Building Adaptive Systems
keathley
38
2.3k
Side Projects
sachag
452
42k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Scaling GitHub
holman
458
140k
The Cost Of JavaScript in 2023
addyosmani
45
6.8k
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