Understanding Prototypal Inheritance

42
Understandin g Prototypal Inheritance Guy Royse @guyroyse

description

One the most misunderstood concepts in JavaScript is prototypal inheritance. Prototypal inheritance is nothing like classical inheritance but is actually quiet easy to grasp once you let go of the bounds of classes and instances. In this session we will explore the nature of JavaScript objects, how they inherit from one another, and why everyone thought this prototype stuff was so hard in the first place. If you want to up your game and really understand how JavaScript inheritance works, come check out this session.

Transcript of Understanding Prototypal Inheritance

Page 1: Understanding Prototypal Inheritance

Understanding Prototypal Inheritance

Guy Royse@guyroyse

Page 2: Understanding Prototypal Inheritance

WhoIs

ThisGuy?

Page 3: Understanding Prototypal Inheritance

RULES OF ENGAGEMENT

Page 4: Understanding Prototypal Inheritance

GetYour

HandsUp!

Page 5: Understanding Prototypal Inheritance

Aren’t Prototypes Hard?

Page 6: Understanding Prototypal Inheritance

What’san

Object?

Page 7: Understanding Prototypal Inheritance

Object or Not?var foo = new Object();var foo = {};var foo = new String(‘foo’);var foo = ‘foo’;var foo = new Number(42);var foo = 42;

var foo = new Boolean(false);var foo = false;var foo = null;var foo = undefined;var foo = function() {};var foo = [1, 2, 3];

Page 8: Understanding Prototypal Inheritance

Objects are Hashesvar alice = {

name : ‘Alice Bone-crusher’,class : ‘Fighter’,level : 5,hitPoints : 57,shield : true

};

Page 9: Understanding Prototypal Inheritance

Reading Propertiesalice.name; //

‘Alice…’

alice[‘class’]; // ‘Fighter’

var property = ‘hitPoints’;alice[property]; //

57;

Page 10: Understanding Prototypal Inheritance

Writing Propertiesalice.name =

‘Alice Spellster’;

alice[‘class’] = ‘Wizard’;

var property = ‘hitPoints’;alice[property] = 24;

Page 11: Understanding Prototypal Inheritance

Adding Propertiesalice.spell = ‘fireball’;

alice[‘spellLevel’] = 3;

var property = ‘damage’;alice[property] = ‘5d6’;

Page 12: Understanding Prototypal Inheritance

Removing Propertiesdelete alice.shield;

delete alice[‘spellLevel’];

var property = ‘damage’;delete alice[property];

Page 13: Understanding Prototypal Inheritance

Undefined & Objectsalice.shield; //

undefined

Page 14: Understanding Prototypal Inheritance

Undefined & Objectsalice.shield = undefined;

alice.shield; // undefined

Page 15: Understanding Prototypal Inheritance

Undefined & Objectsdelete alice.shield;

Page 16: Understanding Prototypal Inheritance

They’re Just Keysalice[‘spell level’] = 3;

alice[‘1234’] = 5;

alice[‘%foo%’] = true;

Page 17: Understanding Prototypal Inheritance

ObjectsAre

Hashes

Page 18: Understanding Prototypal Inheritance

Prototypes

Page 19: Understanding Prototypal Inheritance

What’s a Prototype?

Page 20: Understanding Prototypal Inheritance

How It Workschild.firstName; // “Bobby”child.lastName; // “Tables”child.hasCandy; // true

parent.firstName; // “Bob”parent.lastName; // “Tables”parent.hasCandy; // undefined

Page 21: Understanding Prototypal Inheritance

The Prototype Chain

Page 22: Understanding Prototypal Inheritance

Setting the Prototypevar parent = {};parent.firstName = “Bob”;parent.lastName = “Tables”;

var child = Object.create(parent);child.firstName = “Bobby”;child.hasCandy = true;

Page 23: Understanding Prototypal Inheritance

So Why DidWe Think This

Was So Complex?

Page 24: Understanding Prototypal Inheritance

What’s at the Top?var parent = new Object();parent.firstName = “Bob”; parent.lastName = “Tables”;

var child = Object.create(parent);child.firstName = “Bobby”;child.hasCandy = true;

Page 25: Understanding Prototypal Inheritance

Object.create = function(parent) {var fn = function() {};fn.prototype = parent;return new fn();

};

Page 26: Understanding Prototypal Inheritance

What’sa

Class?

Page 27: Understanding Prototypal Inheritance

Classesare an

InconvenientFiction

Page 28: Understanding Prototypal Inheritance

A Simple Class

var Character = function(name, level) {this.name = name;this.level = level;

};

var alice = new Character(‘Alice’, 5);

Page 29: Understanding Prototypal Inheritance

Anatomy of Character

Characterprototype : {}

functioncall : function() {}apply : function() {}

Object.prototypetoString : function() {}valueOf : function() {}

Character.prototype

Prot

otyp

e Ch

ain var Character = function(name, level) {

this.name = name; this.level = level;}

Page 30: Understanding Prototypal Inheritance

Now Call New

Characterprototype : {}

functioncall : function() {}apply : function() {}

Prot

otyp

e Ch

ain

alicename : ‘Alice’level : 5

new

Character.prototype

Object.prototypetoString : function() {}valueOf : function() {}

var Character = function(name, level) {this.name = name;this.level = level;

}

var alice = new Character(‘Alice’, 5);

Page 31: Understanding Prototypal Inheritance

A More Complex Classvar Character = function(name, level) {

this.name = name;this.level = level;

};

Character.prototype.hitPoints = function() {return this.level * 5;

};

var alice = new Character(alice, 5);

alice.hitPoints(); // 25

Page 32: Understanding Prototypal Inheritance

Anatomy of Character

Characterprototype : {}

functioncall : function() {}apply : function() {}

Object.prototypetoString : function() {}valueOf : function() {}

Character.prototypehitPoints : function() {}

Prot

otyp

e Ch

ain var Character = function(name, level) {

this.name = name; this.level = level;}

Character.prototype.hitPoints = function() { return this.level * 5; };

Page 33: Understanding Prototypal Inheritance

Now Call New

Characterprototype : {}

functioncall : function() {}apply : function() {}

Prot

otyp

e Ch

ain

alicename : ‘Alice’level : 5

new

Character.prototypehitPoints : function() {}

Object.prototypetoString : function() {}valueOf : function() {}

var Character = function(name, level) { this.name = name; this.level = level;}

var alice = new Character(‘Alice’, 5);

Character.prototype.hitPoints = function() { return this.level * 5;};

Page 34: Understanding Prototypal Inheritance

Object.create = function(parent) {var fn = function() {};fn.prototype = parent;return new fn();

};

Page 35: Understanding Prototypal Inheritance

Anatomy of Object.create

fnprototype : {}

functioncall : function() {}apply : function() {}

Object.prototypetoString : function() {}valueOf : function() {}

fn.prototype

Prot

otyp

e Ch

ain var fn = function() {};

fn.prototype = parent;

Page 36: Understanding Prototypal Inheritance

Object.create = function(parent) {var fn = function() {};fn.prototype = parent;return new fn();

};

Page 37: Understanding Prototypal Inheritance

Now Call New

fnprototype : {}

functioncall : function() {}apply : function() {}

Prot

otyp

e Ch

ain

childnew

fn.prototype

Object.prototypetoString : function() {}valueOf : function() {}

return new fn();

var fn = function() {};

fn.prototype = parent;

Page 38: Understanding Prototypal Inheritance

Whew!

Page 39: Understanding Prototypal Inheritance

In Summary

• Objects are Hashes• Use Object.create to define prototypes• Prototypes aren’t hard…

…but the plumbing that makes it work is.

Page 40: Understanding Prototypal Inheritance

Questions?

Page 41: Understanding Prototypal Inheritance

Contact InfoGuy Royse

[email protected]@guyroyse

GuyRoyse.com

Page 42: Understanding Prototypal Inheritance

Image Credits

• http://www.flickr.com/photos/justin_case/1525042316• http://www.flickr.com/photos/tim_d/155441805• http://www.flickr.com/photos/sybrenstuvel/2335168467• http://www.flickr.com/photos/stawarz/3683017780