SlideShare uma empresa Scribd logo
1 de 42
Understanding
Prototypal
Inheritance

Guy Royse
@guyroyse
Who
Is
This
Guy?
RULES OF ENGAGEMENT
Get
Your
Hands
Up!
Aren’t Prototypes Hard?
What’s
an
Object?
Object or Not?
varfoo = new Object();
varfoo = {};
varfoo = new String(‘foo’);
varfoo = ‘foo’;
varfoo = new Number(42);
varfoo = 42;

varfoo = new Boolean(false);
varfoo = false;
varfoo = null;
varfoo = undefined;
varfoo = function() {};
varfoo = [1, 2, 3];
Objects are Hashes
varalice = {
name :
‘Alice Bone-crusher’,
class : ‘Fighter’,
level : 5,
hitPoints : 57,
shield : true
};

‘name’

• ‘Alice Bone-crusher’

‘class’

• ‘Fighter’

‘level’

•5

‘hitPoints’ • 57
‘shield’

• true
Reading Properties
alice.name;

// ‘Alice…’

‘name’

• ‘Alice Bone-crusher’

alice*‘class’+;

// ‘Fighter’

‘class’

• ‘Fighter’

‘level’

•5

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

‘hitPoints’ • 57
‘shield’

• true
Writing Properties
alice.name =
‘Alice Spellster’;

‘name’

• ‘Alice Spellster’

‘class’

• ‘Wizard’

‘level’

•5

alice*‘class’+ = ‘Wizard’;
var property = ‘hitPoints’;
alice[property] = 24;

‘hitPoints’
‘shield’

• 24

• true
Adding Properties
alice.spell = ‘fireball’;

• ‘Wizard’

‘level’

•5

‘hitPoints’

var property = ‘damage’;
alice*property+ = ‘5d6’;

• ‘Alice Spellster’

‘class’

alice*‘spellLevel’+ = 3;

‘name’

• 24

‘shield’

• true

‘spell’

• ‘fireball’

‘spellLevel’ • 3
‘damage’

• ‘5d6’
Removing Properties
delete alice.shield;

• ‘Wizard’

‘level’

•5

‘hitPoints’

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

• ‘Alice Spellster’

‘class’

delete alice*‘spellLevel’+;

‘name’

• 24

‘shield’

• true

‘spell’

• ‘fireball’

‘spellLevel’ • 3
‘damage’

• ‘5d6’
Undefined & Objects
alice.shield; // undefined

‘name’

• ‘Alice Spellster’

‘class’

• ‘Wizard’

‘level’

•5

‘hitPoints’
‘spell’

• 24

• ‘fireball’
Undefined & Objects
alice.shield = undefined;
alice.shield; // undefined

‘name’

• ‘Alice Spellster’

‘class’

• ‘Wizard’

‘level’

•5

‘hitPoints’

• 24

‘spell’

• ‘fireball’

‘shield’

• undefined
Undefined & Objects
delete alice.shield;

‘name’

• ‘Alice Spellster’

‘class’

• ‘Wizard’

‘level’

•5

‘hitPoints’

• 24

‘spell’

• ‘fireball’

‘shield’

• undefined
They’re Just Keys
alice*‘spell level’+ = 3;

• ‘Wizard’

‘level’

•5

‘hitPoints’

alice*‘%foo%’+ = true;

• ‘Alice Spellster’

‘class’

alice*‘1234’+ = 5;

‘name’

• 24

‘spell’

• ‘fireball’

‘spell level’ • 3
‘1234’
‘%foo%’

•5

• true
Objects
Are
Hashes
Prototypes
What’s a Prototype?

child

parent

• firstName : “Bobby”
• hasCandy : true

• firstName : “Bob”
• lastName : “Tables”
How It Works
child.firstName;
child.lastName;
child.hasCandy;

// “Bobby”
// “Tables”
// true

parent.firstName;
parent.lastName;
parent.hasCandy;

// “Bob”
// “Tables”
// undefined

child

parent

• firstName : “Bobby”
• hasCandy : true

• firstName : “Bob”
• lastName : “Tables”
The Prototype Chain

child

parent

grandparent

great_grandparent

• firstName : “Bobby”
• hasCandy : true

• firstName : “Bob”

• firstName : “Robert”
• alive : true

• lastName : “Tables”
• alive: false
Setting the Prototype
var parent = {};
parent.firstName = “Bob”;
parent.lastName = “Tables”;
var child = Object.create(parent);
child.firstName = “Bobby”;
child.hasCandy = true;

child

parent

• firstName : “Bobby”
• hasCandy : true

• firstName : “Bob”
• lastName : “Robertson”
So Why Did
We Think This
Was So Complex?
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;

child

parent

Object.prototype

• firstName : “Bobby”
• hasCandy : true

• firstName : “Bob”
• lastName : “Tables”

• toString : function() {}
• valueOf : function() {}
Object.create = function(parent) {
var fn = function() {};
fn.prototype = parent;
return new fn();
};
What’s
a
Class?
Classes
are an
Inconvenient
Fiction
A Simple Class
var Character = function(name, level) {
this.name = name;
this.level = level;
};
varalice = newCharacter(‘Alice’, 5);
Anatomy of Character

Prototype Chain

Object.prototype
toString : function() {}
valueOf : function() {}

function
call : function() {}
apply : function() {}

Character
prototype : {}

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

Character.prototype
Now Call New

Prototype Chain

Object.prototype
toString : function() {}
valueOf : function() {}
function
call : function() {}
apply : function() {}
Character
prototype : {}

new

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

alice
name : ‘Alice’
level : 5

varalice = new Character(‘Alice’, 5);
A More Complex Class
var Character = function(name, level) {
this.name = name;
this.level = level;
};
Character.prototype.hitPoints = function() {
return this.level * 5;
};
varalice = new Character(alice, 5);
alice.hitPoints(); // 25
Anatomy of Character

Prototype Chain

Object.prototype
toString : function() {}
valueOf : function() {}

function
call : function() {}
apply : function() {}

Character
prototype : {}

var Character = function(name, level) {
this.name = name;
this.level = level;
}
Character.prototype.hitPoints =
function() { return this.level * 5; };
Character.prototype
hitPoints : function() {}
Now Call New

Prototype Chain

Object.prototype
toString : function() {}
valueOf : function() {}
function
call : function() {}
apply : function() {}
Character
prototype : {}

new

var Character = function(name, level) {
this.name = name;
this.level = level;
Character.prototype.hitPoints =
}
function() {
return this.level * 5;
};
Character.prototype
hitPoints : function() {}
alice
name : ‘Alice’
level : 5

varalice = new Character(‘Alice’, 5);
Object.create = function(parent) {
var fn = function() {};
fn.prototype = parent;
return new fn();
};
Anatomy of Object.create

Prototype Chain

Object.prototype
toString : function() {}
valueOf : function() {}

var fn = function() {};
function
call : function() {}
apply : function() {}

fn
prototype : {}

fn.prototype = parent;

fn.prototype
Object.create = function(parent) {
var fn = function() {};
fn.prototype = parent;
return new fn();
};
Now Call New
Object.prototype
toString : function() {}
valueOf : function() {}

Prototype Chain

var fn = function() {};
function
call : function() {}
apply : function() {}
fn
prototype : {}

fn.prototype = parent;

fn.prototype

child
new
return new fn();
Whew!
In Summary
• Objects are Hashes
• Use Object.create to define prototypes
• Prototypes aren’t hard…
…but the plumbing that makes it work is.
Questions?
Contact Info
Guy Royse
guy@guyroyse.com
@guyroyse
GuyRoyse.com
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

Mais conteúdo relacionado

Mais procurados

Sustainable TDD
Sustainable TDDSustainable TDD
Sustainable TDDSteven Mak
 
Drupal 8: A story of growing up and getting off the island
Drupal 8: A story of growing up and getting off the islandDrupal 8: A story of growing up and getting off the island
Drupal 8: A story of growing up and getting off the islandAngela Byron
 
Coffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptCoffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptBrian Mann
 
Why (I think) CoffeeScript Is Awesome
Why (I think) CoffeeScript Is AwesomeWhy (I think) CoffeeScript Is Awesome
Why (I think) CoffeeScript Is AwesomeJo Cranford
 
Clean code with google guava jee conf
Clean code with google guava jee confClean code with google guava jee conf
Clean code with google guava jee confIgor Anishchenko
 
Google Guava - Core libraries for Java & Android
Google Guava - Core libraries for Java & AndroidGoogle Guava - Core libraries for Java & Android
Google Guava - Core libraries for Java & AndroidJordi Gerona
 
Proxies are Awesome!
Proxies are Awesome!Proxies are Awesome!
Proxies are Awesome!Brendan Eich
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScriptNone
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme SwiftMovel
 
Google guava overview
Google guava overviewGoogle guava overview
Google guava overviewSteve Min
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Lukas Ruebbelke
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)James Titcumb
 
Dapper Tool - A Bundle to Make your ECL Neater
Dapper Tool - A Bundle to Make your ECL NeaterDapper Tool - A Bundle to Make your ECL Neater
Dapper Tool - A Bundle to Make your ECL NeaterHPCC Systems
 

Mais procurados (20)

Sustainable TDD
Sustainable TDDSustainable TDD
Sustainable TDD
 
Drupal 8: A story of growing up and getting off the island
Drupal 8: A story of growing up and getting off the islandDrupal 8: A story of growing up and getting off the island
Drupal 8: A story of growing up and getting off the island
 
JavaScript Primer
JavaScript PrimerJavaScript Primer
JavaScript Primer
 
Coffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptCoffeescript: No really, it's just Javascript
Coffeescript: No really, it's just Javascript
 
Why (I think) CoffeeScript Is Awesome
Why (I think) CoffeeScript Is AwesomeWhy (I think) CoffeeScript Is Awesome
Why (I think) CoffeeScript Is Awesome
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
 
Clean code with google guava jee conf
Clean code with google guava jee confClean code with google guava jee conf
Clean code with google guava jee conf
 
Google Guava - Core libraries for Java & Android
Google Guava - Core libraries for Java & AndroidGoogle Guava - Core libraries for Java & Android
Google Guava - Core libraries for Java & Android
 
JavaScript 1.8.5: New Features Explored
JavaScript 1.8.5:  New Features ExploredJavaScript 1.8.5:  New Features Explored
JavaScript 1.8.5: New Features Explored
 
Proxies are Awesome!
Proxies are Awesome!Proxies are Awesome!
Proxies are Awesome!
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Scala in Practice
Scala in PracticeScala in Practice
Scala in Practice
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Google guava overview
Google guava overviewGoogle guava overview
Google guava overview
 
Symfony2 meets propel 1.5
Symfony2 meets propel 1.5Symfony2 meets propel 1.5
Symfony2 meets propel 1.5
 
Getting Testy With Perl6
Getting Testy With Perl6Getting Testy With Perl6
Getting Testy With Perl6
 
Java 8 Examples
Java 8 ExamplesJava 8 Examples
Java 8 Examples
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
 
Dapper Tool - A Bundle to Make your ECL Neater
Dapper Tool - A Bundle to Make your ECL NeaterDapper Tool - A Bundle to Make your ECL Neater
Dapper Tool - A Bundle to Make your ECL Neater
 

Destaque

Programming on Bare Metal: Controlling Circuits with Code
Programming on Bare Metal: Controlling Circuits with CodeProgramming on Bare Metal: Controlling Circuits with Code
Programming on Bare Metal: Controlling Circuits with CodeGuy Royse
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptGuy Royse
 
The Code Christmas Tree: Selling the Investment for Technical Debt
The Code Christmas Tree: Selling the Investment for Technical DebtThe Code Christmas Tree: Selling the Investment for Technical Debt
The Code Christmas Tree: Selling the Investment for Technical DebtGuy Royse
 
Those Who Know History are Doomed to Watch Others Repeat It
Those Who Know History are Doomed to Watch Others Repeat ItThose Who Know History are Doomed to Watch Others Repeat It
Those Who Know History are Doomed to Watch Others Repeat ItGuy Royse
 
Mad Computer Science: Testing COBOL with RSpec
Mad Computer Science: Testing COBOL with RSpecMad Computer Science: Testing COBOL with RSpec
Mad Computer Science: Testing COBOL with RSpecGuy Royse
 
Putting the D&D in TDD
Putting the D&D in TDDPutting the D&D in TDD
Putting the D&D in TDDGuy Royse
 

Destaque (6)

Programming on Bare Metal: Controlling Circuits with Code
Programming on Bare Metal: Controlling Circuits with CodeProgramming on Bare Metal: Controlling Circuits with Code
Programming on Bare Metal: Controlling Circuits with Code
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
 
The Code Christmas Tree: Selling the Investment for Technical Debt
The Code Christmas Tree: Selling the Investment for Technical DebtThe Code Christmas Tree: Selling the Investment for Technical Debt
The Code Christmas Tree: Selling the Investment for Technical Debt
 
Those Who Know History are Doomed to Watch Others Repeat It
Those Who Know History are Doomed to Watch Others Repeat ItThose Who Know History are Doomed to Watch Others Repeat It
Those Who Know History are Doomed to Watch Others Repeat It
 
Mad Computer Science: Testing COBOL with RSpec
Mad Computer Science: Testing COBOL with RSpecMad Computer Science: Testing COBOL with RSpec
Mad Computer Science: Testing COBOL with RSpec
 
Putting the D&D in TDD
Putting the D&D in TDDPutting the D&D in TDD
Putting the D&D in TDD
 

Semelhante a Understanding Prototypal Inheritance

Object-oriented Javascript
Object-oriented JavascriptObject-oriented Javascript
Object-oriented JavascriptDaniel Ku
 
Realm: Building a mobile database
Realm: Building a mobile databaseRealm: Building a mobile database
Realm: Building a mobile databaseChristian Melchior
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Seri Moth
 
GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2Heather Rock
 
06 Map Reduce
06 Map Reduce06 Map Reduce
06 Map Reducecrgwbr
 
__proto__-and-prototype
  __proto__-and-prototype  __proto__-and-prototype
__proto__-and-prototypeLee zhiye
 
Fundamental JS
Fundamental JSFundamental JS
Fundamental JSXiming Dai
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyGautam Rege
 
Front end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreFront end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreWeb Zhao
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix itRafael Dohms
 
FITC CoffeeScript 101
FITC CoffeeScript 101FITC CoffeeScript 101
FITC CoffeeScript 101Faisal Abid
 
Feed Normalization with Ember Data 1.0
Feed Normalization with Ember Data 1.0Feed Normalization with Ember Data 1.0
Feed Normalization with Ember Data 1.0Jeremy Gillick
 
Features of Kotlin I find exciting
Features of Kotlin I find excitingFeatures of Kotlin I find exciting
Features of Kotlin I find excitingRobert MacLean
 
Coffee Scriptでenchant.js
Coffee Scriptでenchant.jsCoffee Scriptでenchant.js
Coffee Scriptでenchant.jsNaoyuki Totani
 
Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012Rafael Dohms
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix itRafael Dohms
 
Tactical DDD patterns in Go
Tactical DDD patterns in GoTactical DDD patterns in Go
Tactical DDD patterns in GoRobert Laszczak
 

Semelhante a Understanding Prototypal Inheritance (20)

Object-oriented Javascript
Object-oriented JavascriptObject-oriented Javascript
Object-oriented Javascript
 
Realm: Building a mobile database
Realm: Building a mobile databaseRealm: Building a mobile database
Realm: Building a mobile database
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2
 
06 Map Reduce
06 Map Reduce06 Map Reduce
06 Map Reduce
 
__proto__-and-prototype
  __proto__-and-prototype  __proto__-and-prototype
__proto__-and-prototype
 
Fundamental JS
Fundamental JSFundamental JS
Fundamental JS
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of ruby
 
Front end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreFront end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript core
 
ECMA 入门
ECMA 入门ECMA 入门
ECMA 入门
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix it
 
FITC CoffeeScript 101
FITC CoffeeScript 101FITC CoffeeScript 101
FITC CoffeeScript 101
 
Feed Normalization with Ember Data 1.0
Feed Normalization with Ember Data 1.0Feed Normalization with Ember Data 1.0
Feed Normalization with Ember Data 1.0
 
Features of Kotlin I find exciting
Features of Kotlin I find excitingFeatures of Kotlin I find exciting
Features of Kotlin I find exciting
 
JavaScript @ CTK
JavaScript @ CTKJavaScript @ CTK
JavaScript @ CTK
 
Coffee Scriptでenchant.js
Coffee Scriptでenchant.jsCoffee Scriptでenchant.js
Coffee Scriptでenchant.js
 
Intro to Perl and Bioperl
Intro to Perl and BioperlIntro to Perl and Bioperl
Intro to Perl and Bioperl
 
Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix it
 
Tactical DDD patterns in Go
Tactical DDD patterns in GoTactical DDD patterns in Go
Tactical DDD patterns in Go
 

Último

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 

Último (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 

Understanding Prototypal Inheritance

  • 7. Object or Not? varfoo = new Object(); varfoo = {}; varfoo = new String(‘foo’); varfoo = ‘foo’; varfoo = new Number(42); varfoo = 42; varfoo = new Boolean(false); varfoo = false; varfoo = null; varfoo = undefined; varfoo = function() {}; varfoo = [1, 2, 3];
  • 8. Objects are Hashes varalice = { name : ‘Alice Bone-crusher’, class : ‘Fighter’, level : 5, hitPoints : 57, shield : true }; ‘name’ • ‘Alice Bone-crusher’ ‘class’ • ‘Fighter’ ‘level’ •5 ‘hitPoints’ • 57 ‘shield’ • true
  • 9. Reading Properties alice.name; // ‘Alice…’ ‘name’ • ‘Alice Bone-crusher’ alice*‘class’+; // ‘Fighter’ ‘class’ • ‘Fighter’ ‘level’ •5 var property = ‘hitPoints’; alice[property]; // 57; ‘hitPoints’ • 57 ‘shield’ • true
  • 10. Writing Properties alice.name = ‘Alice Spellster’; ‘name’ • ‘Alice Spellster’ ‘class’ • ‘Wizard’ ‘level’ •5 alice*‘class’+ = ‘Wizard’; var property = ‘hitPoints’; alice[property] = 24; ‘hitPoints’ ‘shield’ • 24 • true
  • 11. Adding Properties alice.spell = ‘fireball’; • ‘Wizard’ ‘level’ •5 ‘hitPoints’ var property = ‘damage’; alice*property+ = ‘5d6’; • ‘Alice Spellster’ ‘class’ alice*‘spellLevel’+ = 3; ‘name’ • 24 ‘shield’ • true ‘spell’ • ‘fireball’ ‘spellLevel’ • 3 ‘damage’ • ‘5d6’
  • 12. Removing Properties delete alice.shield; • ‘Wizard’ ‘level’ •5 ‘hitPoints’ var property = ‘damage’; delete alice[property]; • ‘Alice Spellster’ ‘class’ delete alice*‘spellLevel’+; ‘name’ • 24 ‘shield’ • true ‘spell’ • ‘fireball’ ‘spellLevel’ • 3 ‘damage’ • ‘5d6’
  • 13. Undefined & Objects alice.shield; // undefined ‘name’ • ‘Alice Spellster’ ‘class’ • ‘Wizard’ ‘level’ •5 ‘hitPoints’ ‘spell’ • 24 • ‘fireball’
  • 14. Undefined & Objects alice.shield = undefined; alice.shield; // undefined ‘name’ • ‘Alice Spellster’ ‘class’ • ‘Wizard’ ‘level’ •5 ‘hitPoints’ • 24 ‘spell’ • ‘fireball’ ‘shield’ • undefined
  • 15. Undefined & Objects delete alice.shield; ‘name’ • ‘Alice Spellster’ ‘class’ • ‘Wizard’ ‘level’ •5 ‘hitPoints’ • 24 ‘spell’ • ‘fireball’ ‘shield’ • undefined
  • 16. They’re Just Keys alice*‘spell level’+ = 3; • ‘Wizard’ ‘level’ •5 ‘hitPoints’ alice*‘%foo%’+ = true; • ‘Alice Spellster’ ‘class’ alice*‘1234’+ = 5; ‘name’ • 24 ‘spell’ • ‘fireball’ ‘spell level’ • 3 ‘1234’ ‘%foo%’ •5 • true
  • 19. What’s a Prototype? child parent • firstName : “Bobby” • hasCandy : true • firstName : “Bob” • lastName : “Tables”
  • 20. How It Works child.firstName; child.lastName; child.hasCandy; // “Bobby” // “Tables” // true parent.firstName; parent.lastName; parent.hasCandy; // “Bob” // “Tables” // undefined child parent • firstName : “Bobby” • hasCandy : true • firstName : “Bob” • lastName : “Tables”
  • 21. The Prototype Chain child parent grandparent great_grandparent • firstName : “Bobby” • hasCandy : true • firstName : “Bob” • firstName : “Robert” • alive : true • lastName : “Tables” • alive: false
  • 22. Setting the Prototype var parent = {}; parent.firstName = “Bob”; parent.lastName = “Tables”; var child = Object.create(parent); child.firstName = “Bobby”; child.hasCandy = true; child parent • firstName : “Bobby” • hasCandy : true • firstName : “Bob” • lastName : “Robertson”
  • 23. So Why Did We Think This Was So Complex?
  • 24. 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; child parent Object.prototype • firstName : “Bobby” • hasCandy : true • firstName : “Bob” • lastName : “Tables” • toString : function() {} • valueOf : function() {}
  • 25. Object.create = function(parent) { var fn = function() {}; fn.prototype = parent; return new fn(); };
  • 28. A Simple Class var Character = function(name, level) { this.name = name; this.level = level; }; varalice = newCharacter(‘Alice’, 5);
  • 29. Anatomy of Character Prototype Chain Object.prototype toString : function() {} valueOf : function() {} function call : function() {} apply : function() {} Character prototype : {} var Character = function(name, level) { this.name = name; this.level = level; } Character.prototype
  • 30. Now Call New Prototype Chain Object.prototype toString : function() {} valueOf : function() {} function call : function() {} apply : function() {} Character prototype : {} new var Character = function(name, level) { this.name = name; this.level = level; } Character.prototype alice name : ‘Alice’ level : 5 varalice = new Character(‘Alice’, 5);
  • 31. A More Complex Class var Character = function(name, level) { this.name = name; this.level = level; }; Character.prototype.hitPoints = function() { return this.level * 5; }; varalice = new Character(alice, 5); alice.hitPoints(); // 25
  • 32. Anatomy of Character Prototype Chain Object.prototype toString : function() {} valueOf : function() {} function call : function() {} apply : function() {} Character prototype : {} var Character = function(name, level) { this.name = name; this.level = level; } Character.prototype.hitPoints = function() { return this.level * 5; }; Character.prototype hitPoints : function() {}
  • 33. Now Call New Prototype Chain Object.prototype toString : function() {} valueOf : function() {} function call : function() {} apply : function() {} Character prototype : {} new var Character = function(name, level) { this.name = name; this.level = level; Character.prototype.hitPoints = } function() { return this.level * 5; }; Character.prototype hitPoints : function() {} alice name : ‘Alice’ level : 5 varalice = new Character(‘Alice’, 5);
  • 34. Object.create = function(parent) { var fn = function() {}; fn.prototype = parent; return new fn(); };
  • 35. Anatomy of Object.create Prototype Chain Object.prototype toString : function() {} valueOf : function() {} var fn = function() {}; function call : function() {} apply : function() {} fn prototype : {} fn.prototype = parent; fn.prototype
  • 36. Object.create = function(parent) { var fn = function() {}; fn.prototype = parent; return new fn(); };
  • 37. Now Call New Object.prototype toString : function() {} valueOf : function() {} Prototype Chain var fn = function() {}; function call : function() {} apply : function() {} fn prototype : {} fn.prototype = parent; fn.prototype child new return new fn();
  • 38. Whew!
  • 39. In Summary • Objects are Hashes • Use Object.create to define prototypes • Prototypes aren’t hard… …but the plumbing that makes it work is.

Notas do Editor

  1. NameTechnologiesCompanyCBusJS
  2. Rules of EngagementInterrupt me-80/20 ruleWe’re gonna go deep (up to the edge of what I know)- not going to talk about browsers – this is about the language
  3. Poll the audience
  4. Not really… in some ways their actually quite easy but first you have to understand objects
  5. Poll the audience… tell the smarties to keep quiet
  6. Undefined exists, I suspect, because null is empty while undefined is not in the hash, but…
  7. …of course you can do this. Which pretty much undermines that premise. Can’t delete by setting property to undefined
  8. Every object has a prototype (which is another object)Note I said “has a” and not “is a” – this is compositionIf it ain’t found in the child, look in the parent
  9. At this point we know how to use prototypal inheritanceNOTE the empty object here
  10. Because of the new operator and the prototype property
  11. We didn’t have a new operator…. but we had an implied one.object is an implied root, can be accessed via Object.prototype
  12. It’s inside of the definition of Object.create as well.new suggests classes and classical inheritance while prototype suggests that stuff we were talking about earlierThis doesn’t quite jive, So let’s look at classes
  13. Poll the audience
  14. Not real, a confusing layer on top of an otherwise pleasant functional language.People always contradict me on this point… but psuedo classes aren’t classes… that’s why the psuedo is there
  15. Where’s the word class… looks like a function to me. But, we do have the keyword newIt’s really a constructor function the makes objects … classes are templates. JavaScript doesn’t have those, just these functions that you call with new(and make sure you call new ‘cuz otherwise, bad stuff can happen)
  16. NOTE the function on Character.prototypeWe are adding something to the prototype… it’s just an object, we can do that easily.In fact you can do this with any “class” in Javascript including Number, Array, Boolean, String, and Object
  17. Remember this guy? Now we have the tools to understand how he works…Note that we are *replacing* the prototype of this constructor function as opposed to adding to it or just taking it as isAlso note the grand utility of the fn function … it doesn’t do much .. In fact, it’s just there to give use something with a prototype property we can call new against
  18. We are reassigning the prototype property of the function… yes you can do this.
  19. NameTechnologiesCompanyCBusJS