classes are premature optimization: prototypes in javascript · the past, present, and future of...

169
Classes are Premature Optimization: Prototypes in Javascript Justin Love http://pinboard.in/u:wondible/t:prototypal/ http://pinboard.in/u:wondible/t:oop/

Upload: others

Post on 10-Oct-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Classes are Premature Optimization:

Prototypes in JavascriptJustin Love

http://pinboard.in/u:wondible/t:prototypal/http://pinboard.in/u:wondible/t:oop/

Page 3: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Outline

• OOP

• Classical

• Prototypal

• Objects in Javascript

• Patterns and performance

• Review

Page 4: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

OOP?

Page 5: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

class ____ {

...

}

Page 6: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

object ____ {

...

}

Page 7: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Class Oriented?

class ____ {

...

}

Page 8: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

OOP-or-

COP?

Page 9: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

OOP

Page 10: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Those who do not remember history are condemned to repeat it.

George Santayana

Page 11: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

(Chances are I’m repeating it right now.)

Page 12: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Alan Kay

Page 13: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

3things

OOP

Page 14: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

EncapsulationLocal

RetentionProtection

Hidingof State-process

Page 15: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Genericity

Commonly, PolymophisimAlan Kay spoke of Algebras

Extreme late binding

Page 16: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Messaging

Object decides how to react.

Page 17: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

OOP

EncapsulationGenericityMessaging

Page 18: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Classes

Page 19: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Inheritance

Page 20: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

“Five words

objectmessageclassinstancemethod

make up the vocabulary with which Smalltalk

is discussed.”

Smalltalk-80, The Language, p.51

Page 21: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Smalltalk 71727680

Classes

Inheritance

Metaclasses

Page 22: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Alan Kay2003

“So I decided to leave out inheritance as a built-in feature until I understood it better.”

Page 23: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Alan Kay2003

“[...] backslid towards Simula and did not replace the extension mechanisms with safer ones that were anywhere near as useful.”

Page 24: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Classical

Page 25: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Sketchpad

1963

Page 26: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Generalvs.

Specific

Page 27: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Simula-67

C++Javaetc.

Page 28: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Classes Instances

Page 29: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Segregation

Page 30: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Plato

Formvs.

Matter

Page 31: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Descriptionvs.

Representation

Page 32: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Classes Instances

Page 33: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in
Page 34: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Metaclass

“Class of classes”

Page 35: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in
Page 36: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in
Page 37: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

RubyRuby Logo © 2006, Yukihiro Matsumoto

Page 38: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

I’m not smart enough for Ruby’s

object system.

Page 39: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Peter DeutschThe Past, Present, and Future of Smalltalk

1989

“metaclasses have proven confusing to many users, and perhaps in the balance more confusing than valuable.”

Page 40: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Accidental Complexity

Page 41: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

William of Ockham

“entia non sunt multiplicanda praeter necessitatem”

Page 42: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

William of Ockham

“entities must not be multiplied beyond necessity”

Page 43: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Prototypal

Page 44: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Henry LiebermanOOPSLA 1986

http://web.media.mit.edu/~lieber/Lieberary/OOP/Delegation/Delegation.html

Using Prototypical Objects to Implement Shared Behavior in Object Oriented Systems

Page 45: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Self

1987

Page 46: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in
Page 47: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in
Page 48: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in
Page 49: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in
Page 50: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Abstractions

Objects

(Classes)

Page 51: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Objects

Abstractions?

Page 52: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

one

one = Object.create(Object.prototype)

Page 53: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

one two

two = Object.create(one)

Page 54: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

one two

two = Object.create(one)

Beware ofmutable state!

Page 55: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

P

one two three .....

one = Object.create(ptoto)two = Object.create(proto)

three = Object.create(proto)

Page 56: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

YouAin’tGonnaNeed It

Page 57: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Objects in Javascript

Page 58: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

What’s an object?

Page 59: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

An object is a

Hash Table

Page 60: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

An object is a

Hash TableMap

Page 61: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

An object is a

Hash TableMap

Implementation detail

Page 62: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Map

a x

b y

c z

Page 63: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Array

0 x

1 y

2 z

Page 64: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Array

number value

Page 65: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Object

string value

Page 66: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Syntactic Sugar

object[‘string’]

object.string

Page 67: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Prototypes

Page 68: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Objects can be related

Page 69: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Reference

Page 70: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Every object has a

prototype

Page 71: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Prototype

What You’ve GotIt’s Prototype

Objects

Page 72: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

ECMAScript Spec

[[Prototype]]

Page 73: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Mozilla

.__proto__

Page 74: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

ECMAScript 5

Object.getPrototypeOf(instance)

Page 75: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Read

= x.b

Page 76: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Read

= x.a

Page 77: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Read

= x.toString

Page 78: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Write

x.a = “C”

Page 79: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Readback

= x.a

Page 80: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Compound Objects

Page 81: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

“Classes”

Page 82: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

new Class();

Syntatic Sugur

Page 83: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Leaky Abstraction

Page 84: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Object.create()

• ES5

• Firefox 4

• Safari 5

• Chrome 5

• IE 9

• http://kangax.github.com/es5-compat-table/

Page 85: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Object.create()

Object.create = function(o) {

function F() {};

F.prototype = (o || {});

return new F();

}

http://javascript.crockford.com/prototypal.html

Page 86: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

P

one two three .....

one = Object.create(ptoto)two = Object.create(proto)

three = Object.create(proto)

Page 87: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Singleton

Page 88: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Singleton (in JS)

var singleton = {

...

};

Page 89: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Ex-Nihilo

“Out of Nothing”

Page 90: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Singleton: Self

clone = (self)

Page 91: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Prototypal Singleton

singleton.clone = function() {

return this;

};

Page 92: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Hard-core Singleton

function Singleton() {

return Singleton.prototype;

}

Page 93: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Near Miss

Page 94: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Near Miss

Page 95: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Near MissDay

Kin

Page 96: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Near Miss

var kin = day.clone("Kin");

Page 97: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Near Miss

clone: function(name) {

var o = Object.create(this);

o.name = name;

...

return o;

},

Page 98: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Near Miss

...

// independent existence.

o.scale = 0;

o.position = 0;

o.time = 0;

...

Page 99: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Nested Context

Page 100: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Nested Context

Like {block scope}.

(Or function scope in Javascript.)

Page 101: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Nested Context

under: function(path, f) {

var m = Object.create(this);

m.cd(path);

f(m);

},

Page 102: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Performance

Page 103: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Structured Memory

Page 104: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Programming as Experience:The Inspiration for Self

1995

Self runs

“2.3 times slower than optimized C++.”

Page 105: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Self includes: SmalltalkMario Wolczko

“all [sample] Smalltalk programs run faster in the the Self system, some significantly more so.”

Page 106: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Omega

1990Statically-typed prototypes

Page 107: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Design-Timevs.

Run-Time

Page 108: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Kevo

1993Concatative

‘Clone families’

Page 109: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Lisaac

2003Compiled.

44% slower to 17% fasterthan c.

http://www.lisaac.org/documentation/benchmarks

Page 110: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

V8 Logo © 2006 Google

V82008

Page 111: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

V8 Logo © 2006 Google

Generated Code

Page 112: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

V8 Logo © 2006 Google

Hidden Classes

Page 113: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Javascript

1996

Page 114: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Javascript

Is not an Object-Oriented Programming Language

Page 115: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Genericity

PASS

Page 116: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Messaging

FAIL*Partial support in ES5 and Harmony

Page 117: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Encapsulation

FAIL*Work-around with closure.

Page 118: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Peter DeutschThe Past, Present, and Future of Smalltalk

1989

“inheriting implementation [...] tends to create difficulties in subsequent evolution and often reflects insufficient understanding.”

Page 119: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Copyvs.

Delegation

Page 120: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Data

Page 121: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Data

Page 122: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Data

Page 123: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Data

......

...

...

......

Page 124: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Seph

Stateless Delgation

http://olabini.com/blog/2010/07/preannouncing-seph/

Page 125: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Sparse Objects

Page 126: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Sparse Objects

Page 127: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Sparse Objects

new Disk('Minute',

unit.MS_S,

1,

T("seconds/minute"), {

colors: '#d22'

})

Page 128: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Sparse Objectsnew Disk('Solar Year',

unit.MS_D,

T("days/~month"),

12, {

colors: ['blue', 'green',

'orange', 'brown'],

...

Page 129: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Sparse Objects

...

majorNames: ['~January'...],

subDivide: 4,

major: 3*4,

median: 1*4,

...

Page 130: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Sparse Objects

...

render: function(context) {

context.save();

context.rotate(

6.28 * -9 / 365);

...

Page 131: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Flyweight

Page 132: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Flyweight

Page 133: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Flyweight

Page 134: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Flyweight

Page 135: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Flyweight

item: function(d) {

var i = Object.create(this);

i.dot = d;

return i;

},

Page 136: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Behavior

Page 137: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Behavior

Page 138: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Behavior

Page 139: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Behaviormint: function(

head, pattern, action) {

var my = this.dup();

my.head = head;

my.pattern = pattern;

my.action = action;

return my;

},

Page 140: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Behavior

dup: function() {

return Object.create(this);

},

Page 141: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

*

*

*

Page 142: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Prototype

Instances

Page 143: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Do I use class patterns?

Page 144: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

YES

Page 145: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Do I use class patterns

for everything?

Page 146: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

NO

Page 147: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

a

Objects

Classes

Page 148: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

a

Self

Smalltalk

Page 149: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Classesare aUsefulPattern

Page 150: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

But notthe onlyPattern

Page 151: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Use Classes

Because You Want To

Page 152: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Use Classes

Because You Have To

Not

Page 153: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Review

Page 154: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

History

Page 155: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

OOP

Page 156: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Classical

Page 157: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Prototypal

Page 158: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Performance

Page 159: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Object

string value

Page 160: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Prototype

What You’ve GotIt’s Prototype

Objects

Page 161: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

We’re Stuck with

Javascript

Page 162: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Learn How to Use It

Page 163: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Prototype

Instances

Page 164: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Behavior

Page 165: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Shared Data

Page 166: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Singleton

Page 167: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Flyweight

Page 168: Classes are Premature Optimization: Prototypes in Javascript · The Past, Present, and Future of Smalltalk 1989 “metaclasses have proven confusing to many users, and perhaps in

Nested Context