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

Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Outline

• OOP

• Classical

• Prototypal

• Objects in Javascript

• Patterns and performance

• Review

Page 4: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

OOP?

Page 5: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

class ____ {

...

}

Page 6: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

object ____ {

...

}

Page 7: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Class Oriented?

class ____ {

...

}

Page 8: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

OOP-or-

COP?

Page 9: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

OOP

Page 10: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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

George Santayana

Page 11: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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

Page 12: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Alan Kay

Page 13: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

3things

OOP

Page 14: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

EncapsulationLocal

RetentionProtection

Hidingof State-process

Page 15: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Genericity

Commonly, PolymophisimAlan Kay spoke of Algebras

Extreme late binding

Page 16: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Messaging

Object decides how to react.

Page 17: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

OOP

EncapsulationGenericityMessaging

Page 18: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Classes

Page 19: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Inheritance

Page 20: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

“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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Smalltalk 71727680

Classes

Inheritance

Metaclasses

Page 22: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Classical

Page 25: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Sketchpad

1963

Page 26: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Generalvs.

Specific

Page 27: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Simula-67

C++Javaetc.

Page 28: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Classes Instances

Page 29: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Segregation

Page 30: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Plato

Formvs.

Matter

Page 31: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Descriptionvs.

Representation

Page 32: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Classes Instances

Page 33: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989
Page 34: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Metaclass

“Class of classes”

Page 35: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989
Page 36: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989
Page 37: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

RubyRuby Logo © 2006, Yukihiro Matsumoto

Page 38: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

I’m not smart enough for Ruby’s

object system.

Page 39: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Accidental Complexity

Page 41: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

William of Ockham

“entia non sunt multiplicanda praeter necessitatem”

Page 42: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

William of Ockham

“entities must not be multiplied beyond necessity”

Page 43: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Prototypal

Page 44: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Self

1987

Page 46: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989
Page 47: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989
Page 48: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989
Page 49: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989
Page 50: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Abstractions

Objects

(Classes)

Page 51: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Objects

Abstractions?

Page 52: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

one

one = Object.create(Object.prototype)

Page 53: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

one two

two = Object.create(one)

Page 54: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

one two

two = Object.create(one)

Beware ofmutable state!

Page 55: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

P

one two three .....

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

three = Object.create(proto)

Page 56: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

YouAin’tGonnaNeed It

Page 57: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Objects in Javascript

Page 58: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

What’s an object?

Page 59: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

An object is a

Hash Table

Page 60: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

An object is a

Hash TableMap

Page 61: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

An object is a

Hash TableMap

Implementation detail

Page 62: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Map

a x

b y

c z

Page 63: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Array

0 x

1 y

2 z

Page 64: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Array

number value

Page 65: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Object

string value

Page 66: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Syntactic Sugar

object[‘string’]

object.string

Page 67: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Prototypes

Page 68: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Objects can be related

Page 69: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Reference

Page 70: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Every object has a

prototype

Page 71: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Prototype

What You’ve GotIt’s Prototype

Objects

Page 72: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

ECMAScript Spec

[[Prototype]]

Page 73: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Mozilla

.__proto__

Page 74: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

ECMAScript 5

Object.getPrototypeOf(instance)

Page 75: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Read

= x.b

Page 76: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Read

= x.a

Page 77: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Read

= x.toString

Page 78: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Write

x.a = “C”

Page 79: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Readback

= x.a

Page 80: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Compound Objects

Page 81: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

“Classes”

Page 82: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

new Class();

Syntatic Sugur

Page 83: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Leaky Abstraction

Page 84: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

P

one two three .....

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

three = Object.create(proto)

Page 87: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Singleton

Page 88: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Singleton (in JS)

var singleton = {

...

};

Page 89: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Ex-Nihilo

“Out of Nothing”

Page 90: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Singleton: Self

clone = (self)

Page 91: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Prototypal Singleton

singleton.clone = function() {

return this;

};

Page 92: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Hard-core Singleton

function Singleton() {

return Singleton.prototype;

}

Page 93: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Near Miss

Page 94: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Near Miss

Page 95: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Near MissDay

Kin

Page 96: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Near Miss

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

Page 97: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Near Miss

clone: function(name) {

var o = Object.create(this);

o.name = name;

...

return o;

},

Page 98: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Near Miss

...

// independent existence.

o.scale = 0;

o.position = 0;

o.time = 0;

...

Page 99: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Nested Context

Page 100: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Nested Context

Like {block scope}.

(Or function scope in Javascript.)

Page 101: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Nested Context

under: function(path, f) {

var m = Object.create(this);

m.cd(path);

f(m);

},

Page 102: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Performance

Page 103: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Structured Memory

Page 104: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Omega

1990Statically-typed prototypes

Page 107: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Design-Timevs.

Run-Time

Page 108: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Kevo

1993Concatative

‘Clone families’

Page 109: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Lisaac

2003Compiled.

44% slower to 17% fasterthan c.

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

Page 110: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

V8 Logo © 2006 Google

V82008

Page 111: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

V8 Logo © 2006 Google

Generated Code

Page 112: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

V8 Logo © 2006 Google

Hidden Classes

Page 113: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Javascript

1996

Page 114: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Javascript

Is not an Object-Oriented Programming Language

Page 115: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Genericity

PASS

Page 116: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Messaging

FAIL*Partial support in ES5 and Harmony

Page 117: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Encapsulation

FAIL*Work-around with closure.

Page 118: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Copyvs.

Delegation

Page 120: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Shared Data

Page 121: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Shared Data

Page 122: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Shared Data

Page 123: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Shared Data

......

...

...

......

Page 124: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Seph

Stateless Delgation

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

Page 125: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Sparse Objects

Page 126: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Sparse Objects

Page 127: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Sparse Objects

new Disk('Minute',

unit.MS_S,

1,

T("seconds/minute"), {

colors: '#d22'

})

Page 128: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Sparse Objects

...

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

subDivide: 4,

major: 3*4,

median: 1*4,

...

Page 130: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Sparse Objects

...

render: function(context) {

context.save();

context.rotate(

6.28 * -9 / 365);

...

Page 131: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Flyweight

Page 132: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Flyweight

Page 133: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Flyweight

Page 134: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Flyweight

Page 135: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Flyweight

item: function(d) {

var i = Object.create(this);

i.dot = d;

return i;

},

Page 136: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Shared Behavior

Page 137: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Shared Behavior

Page 138: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Shared Behavior

Page 139: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

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 Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Shared Behavior

dup: function() {

return Object.create(this);

},

Page 141: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

*

*

*

Page 142: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Prototype

Instances

Page 143: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Do I use class patterns?

Page 144: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

YES

Page 145: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Do I use class patterns

for everything?

Page 146: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

NO

Page 147: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

a

Objects

Classes

Page 148: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

a

Self

Smalltalk

Page 149: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Classesare aUsefulPattern

Page 150: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

But notthe onlyPattern

Page 151: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Use Classes

Because You Want To

Page 152: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Use Classes

Because You Have To

Not

Page 153: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Review

Page 154: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

History

Page 155: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

OOP

Page 156: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Classical

Page 157: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Prototypal

Page 158: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Performance

Page 159: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Object

string value

Page 160: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Prototype

What You’ve GotIt’s Prototype

Objects

Page 161: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

We’re Stuck with

Javascript

Page 162: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Learn How to Use It

Page 163: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Prototype

Instances

Page 164: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Shared Behavior

Page 165: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Shared Data

Page 166: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Singleton

Page 167: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Flyweight

Page 168: Classes are Premature Optimization: Prototypes in Javascriptwondible.com/pub/protoint-fvcp.pdf · object system. Peter Deutsch The Past, Present, and Future of Smalltalk 1989

Nested Context