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
Learning to Fly — Twitter Flight and Mixins
Search
Angus Croll
August 23, 2013
Technology
23
4.5k
Learning to Fly — Twitter Flight and Mixins
Presented at BrazilJS, Porto Alegre, August 22nd 2013
Angus Croll
August 23, 2013
Tweet
Share
More Decks by Angus Croll
See All by Angus Croll
The Book Nerd's Guide to JavaScript
anguscroll
14
4.1k
Stop being Perfect
anguscroll
11
1.4k
ES6 Uncensored
anguscroll
72
12k
JavaScript is Literature is JavaScript
anguscroll
21
3.3k
The Politics of JavaScript
anguscroll
122
23k
Parlez-Vous JavaScript
anguscroll
33
6.3k
Break all the Rulez
anguscroll
42
15k
How we Learned to Stop Worrying and Love JavaScript
anguscroll
90
21k
Other Decks in Technology
See All in Technology
Password-less Journey - パスキーへの移行を見据えたユーザーの準備 @ AXIES 2024
ritou
3
1.4k
Kubeshark で Kubernetes の Traffic を眺めてみよう/Let's Look at k8s Traffic with Kubeshark
kota2and3kan
3
370
10分で学ぶKubernetesコンテナセキュリティ/10min-k8s-container-sec
mochizuki875
3
320
フロントエンド設計にモブ設計を導入してみた / 20241212_cloudsign_TechFrontMeetup
bengo4com
0
1.9k
レンジャーシステムズ | 会社紹介(採用ピッチ)
rssytems
0
150
1等無人航空機操縦士一発試験 合格までの道のり ドローンミートアップ@大阪 2024/12/18
excdinc
0
150
kargoの魅力について伝える
magisystem0408
0
200
How to be an AWS Community Builder | 君もAWS Community Builderになろう!〜2024 冬 CB募集直前対策編?!〜
coosuke
PRO
2
2.8k
Storage Browser for Amazon S3
miu_crescent
1
120
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
210
Oracle Cloudの生成AIサービスって実際どこまで使えるの? エンジニア目線で試してみた
minorun365
PRO
4
270
Amazon SageMaker Unified Studio(Preview)、Lakehouse と Amazon S3 Tables
ishikawa_satoru
0
150
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
It's Worth the Effort
3n
183
28k
Rails Girls Zürich Keynote
gr2m
94
13k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.2k
Making the Leap to Tech Lead
cromwellryan
133
9k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
Thoughts on Productivity
jonyablonski
67
4.4k
Optimizing for Happiness
mojombo
376
70k
Agile that works and the tools we love
rasmusluckow
328
21k
Transcript
Learning to fly Twitter Flight and Mixins @angustweets Thursday, August
22, 13
the most difficult part of JS inheritance... And on the
6th day, God created an abundance of Talking Animals, that they may be used in JavaScript inheritance examples. Thursday, August 22, 13
So, talking animals it is! meow!! Thursday, August 22, 13
Flight flightjs.github.io An event driven web framework by Twitter Thursday,
August 22, 13
Flight powers... twitter.com tweetdeck gumroad yinzcam crashalytics and more... Thursday,
August 22, 13
A set of components which bind functionality to DOM Nodes...
Thursday, August 22, 13
github.com/flightjs/example-app Thursday, August 22, 13
github.com/flightjs/example-app Thursday, August 22, 13
Components are completely decoupled and communicate via event bubbling Thursday,
August 22, 13
Thursday, August 22, 13
Components often share behaviours Thursday, August 22, 13
Components often share behaviours So we needed a re-use technique
Thursday, August 22, 13
Inheritance classical composition mixins Functions Object.create Constructor.prototype function passing /
callbacks Objects call/apply functional mixins (Some) JavaScript Re-use Patterns Thursday, August 22, 13
Inheritance classical composition mixins Functions Object.create Constructor.prototype function passing /
callbacks Objects call/apply functional mixins (Some) JavaScript Re-use Patterns Thursday, August 22, 13
Classical Inheritance Thursday, August 22, 13
ES 6 is giving us class. Some libraries have their
own versions. Thursday, August 22, 13
implementation varies widely and abstraction is often leaky (const, static)
class, extend and super are reserved words in particular, the relationship between this and super is difficult... Thursday, August 22, 13
//Prototype.js var SwimmingAnimal = Class.create(Animal, { type: 'SwimmingAnimal', speak: function($super)
{ return $super() + ", glug"; } }); //https://github.com/ded/klass var Alien = SuperHuman.extend({ speak: function() { this.supr(); //speak like a SuperHuman } }); Thursday, August 22, 13
Do you even want classes? Thursday, August 22, 13
Animal Walking Animal Swimming Animal Flying Animal Cat Elephant Crocodile
Whale Eagle Bat Egg-laying Animal Migrating Animal Thursday, August 22, 13
Crocodile Animal Walking Animal Swimming Animal Flying Animal Cat Elephant
Whale Eagle Bat Egg-laying Animal Migrating Animal Thursday, August 22, 13
Crocodile Animal Walking Animal Swimming Animal Flying Animal Cat Elephant
Whale Eagle Bat Egg-laying Animal Migrating Animal WTF? I walk too!! Thursday, August 22, 13
Animal Walking Animal Swimming Animal Flying Animal Cat Elephant Crocodile
Whale Eagle Bat Egg-laying Animal Migrating Animal Thursday, August 22, 13
Duck? Animal Walking Animal Swimming Animal Flying Animal Cat Elephant
Crocodile Whale Eagle Bat Egg-laying Animal Migrating Animal Thursday, August 22, 13
Duck? Animal Cat Elephant Crocodile Whale Eagle Bat Thursday, August
22, 13
Duck? Animal Cat Elephant Crocodile Whale Eagle Bat Walking Animal
Swimming Animal Flying Animal Egg-laying Animal Migrating Animal Thursday, August 22, 13
Classical inheritance Requires up-front knowledge of general case Single rooted
hierarchy We’re really bad at classification Thursday, August 22, 13
Constructor.prototype chaining Thursday, August 22, 13
awkward and unnatural Thursday, August 22, 13
var Animal = function(gender, says) { this.gender = gender; this.says
= says; }; var WalkingAnimal = function(gender, says) { Animal.call(this, gender, says); } WalkingAnimal.prototype = new Animal(); Thursday, August 22, 13
...and new Constructor syntax is classical inheritance Thursday, August 22,
13
Beyond classification... Thursday, August 22, 13
19th century British philosophers Whewell and Jevons argued that there
are no objectively “right” classifications Thursday, August 22, 13
“In general, when working with prototypes, one typically chooses not
to categorize but to exploit alikeness.” –Antero Taivalsaari (Nokia Research Center) Thursday, August 22, 13
liberates prototype chaining from classification Object.create Thursday, August 22, 13
var animalProto = { speak: function() { console.log(this.says); } }
var walkingAnimalProto = Object.create(animalProto); // then augment walkingAnimalProto here... Thursday, August 22, 13
but, that second argument :’’( Thursday, August 22, 13
var animalProto = { speak: function() { console.log(this.says); } }
var walkingAnimalProto = Object.create( animalProto, { walk: { value: function() { console.log('walking'); } } //... } ); Thursday, August 22, 13
and inheritance is still single rooted Thursday, August 22, 13
The sad thing is that we even care about inheritance
hierarchies... Thursday, August 22, 13
The sad thing is that we even care about inheritance
hierarchies... ...because in JavaScript they’re quite unnecessary... Thursday, August 22, 13
JavaScript has first class functions Any function can be assigned
to any object Thursday, August 22, 13
Mixins Thursday, August 22, 13
//A property copy mixin var withWalking = { walk: function()
{ console.log('walking'); }, turn: function(direction) { console.log('turning', direction); }, stopWalking: function() { console.log('stopped walking'); } }; Thursday, August 22, 13
var extend = function(destination, source) { for (var k in
source) { if (source.hasOwnProperty(k)) { destination[k] = source[k]; } } return destination; }; extend(Crocodile.prototype, withWalking); extend(Crocodile.prototype, withSwimming); Thursday, August 22, 13
No hierarchical constraints Functionality is grouped by what it does
not who it belongs to Define special cases first; common code later Thursday, August 22, 13
Mixin must know about target Properties can only clobber But...
Thursday, August 22, 13
Functional Mixins Thursday, August 22, 13
var withWalking = function() { this.walk = function() { console.log('walking');
}; this.turn = function(direction) { console.log('turning', direction); }; this.stopWalking = function() { console.log('stopped walking'); }; }; Thursday, August 22, 13
function Crocodile(name, gender) { this.name = name; this.gender = gender;
} Crocodile.prototype.stalkTourists = function() { //.. }; withWalking.call(Crocodile.prototype); withSwimming.call(Crocodile.prototype); Thursday, August 22, 13
Flight uses functional mixins Thursday, August 22, 13
github.com/flightjs/example-app Thursday, August 22, 13
function withSelect() { this.defaultAttrs({ selectedIds: [] }); this.selectItem = function($item,
append) { $item.addClass(this.attr.selectedClass); this.attr.selectedIds.push($item.attr('id')); this.trigger('uiSelectionChanged', {selIds: this.attr.selectedIds}); }; this.unselectItem = function($item) { $item.removeClass(this.attr.selectedClass); this.attr.selectedIds.splice(getIdIndex($item), 1); this.trigger('uiSelectionChanged', {selIds: this.attr.selectedIds}); }; } Thursday, August 22, 13
defineComponent(mailItems, withSelect); defineComponent(folders, withSelect); defineComponent(moveToSelector, withSelect); Thursday, August 22, 13
The Advice Mixin Thursday, August 22, 13
advice.js is a mixin Thursday, August 22, 13
advice.js is a mixin it lets you add custom code
before(), after() or around() an existing function. Thursday, August 22, 13
all flight mixins get advice.js for free Thursday, August 22,
13
all flight mixins get advice.js for free so mixins can
augment functions, not clobber them Thursday, August 22, 13
withAdvice.call(Crocodile.prototype); function withFlu() { this.before('walk', function() { console.log('sniff'); }); }
var sickCrocodile = new Crocodile(); withFlu.call(sickCrocodile); sickCrocodile.walk(); //"sniff, pad, pad, pad, pad" Thursday, August 22, 13
function withSelect() { this.defaultAttrs({ selectedIds: [] }); this.toggleItemSelect = function(ev,
data) { var $item = $(data.el), append; if ($item.hasClass(this.attr.selectedClass)) { this.unselectItem($item); } else { this.selectItem($item, isMultiSelect(ev)); } }; this.selectItem = function($item, append) {/*..*/}; this.unselectItem = function($item) {/*..*/}; this.after('initialize', function() { this.on(this.select('itemSelector'), 'click', this.toggleItemSelect); }); } Thursday, August 22, 13
So why do we like functional mixins? Thursday, August 22,
13
Mixins as verbs instead of nouns. Mixins are functions. We
can take advantage of closure scope and arguments. A mixin can be applied to any object type: prototype, instance, whatever. Advice allows functional mixins to augment existing functions, not clobber them. Thursday, August 22, 13
Works with the language Simple to understand No surprises! Thursday,
August 22, 13
Obrigado! @angustweets javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at- javascript-mixins/ Thursday, August 22, 13