SlideShare a Scribd company logo
JavaScript Training
Goal Trainers Format
• Lecture
• Exercises
• Ask Questions!
• bitovi/js-training
Prototypal Inheritance
Shared Properties
Animal = function(){
this.offspring = [];
}
Dog = function(){}
Dog.prototype = new Animal()
var dog1 = new Dog(),
dog2 = new Dog(),
pup = new Dog();
dog1.offspring.push(pup);
dog2.offspring; // -> ?
Prototype methods
Animal = function(name) {
this.name = name;
}
Animal.prototype.eats = function(){
return this.name + " is eating."
}
sponge = new Animal(“Bob”)
name
Bob
__proto__
sponge
prototype
fneatsAnimal
PrototypefnAnimal
Prototypal Inheritance
Mammal Chordat
e
Animal
Mammal
Prototype
Chordate
Prototype
Animal
Prototype
prototype has_spinehas_hair eats
prototype prototype
proto proto
Proto-chaining
Mammal.prototype.has_hair = true;
Chordate.prototype = new Animal();
Animal = function(name) { this.name = name }
Animal.prototype.eats = function(){
return this.name+" is eating."
}
Chordate = function(name){ this.name = name; }
Chordate.prototype.has_spine = true;
Mammal = function(name){ this.name = name; }
Mammal.prototype = new Chordate();
m = new Mammal(‘dog’);
m
Object
proto
has_hair
Mammal Chordat Animal
Mammal
Prototype
Chordate
Prototype
Animal
Prototype
prototype
has_spine eats
prototype prototype
proto proto
Animal
Object
Chordate
Object
Proto-chaining
Mammal.prototype.has_hair = true;
Chordate.prototype = new Animal();
Animal = function(name) { this.name = name }
Animal.prototype.eats = function(){
return this.name+" is eating."
}
Chordate = function(name){ Animal.call(this, name); }
Chordate.prototype.has_spine = true;
Mammal = function(name){ Chordate.call(this, name); }
Mammal.prototype = new Chordate();
m = new Mammal(‘dog’);
m
Object
proto
has_hair
Mammal Chordat Animal
Mammal
Prototype
Chordate
Prototype
Animal
Prototype
prototype
has_spine eats
prototype prototype
proto proto
Animal
Object
Chordate
Object
Shared Properties
Animal = function(){
this.offspring = [];
}
Dog = function(){}
Dog.prototype = new Animal()
var dog1 = new Dog(),
dog2 = new Dog(),
pup = new Dog();
dog1.offspring.push(pup);
dog2.offspring; // -> pup
Animal
PROTOTYPE
PROTOTYPE OFFSPRING
dog1 dog2 pup
Dog
_proto_ _proto_
_proto_
_proto_
Exercise
Write the new operator as if it was implemented in JS.
var Person = function(name) {
this.name = name;
}
Person.prototype.speak = function(){ console.log(‘Hello!’) }
var person = NEW( Person, ['name'] );
// var person = new Person(‘name’)
person.speak();
Hints:
• NEW takes the constructor function and an array of arguments
Summary
var Dog = function() {};
var pup = new Dog();
fn OBJECT
OBJECT
PROTOTYPE
pup
Dog
Object.create
Animal = {
init: function(name) {
this.name = name;
},
eats: function() {
return this.name + " is eating."
}
}
Chordate = Object.create(Animal, {
has_spine: { value: true }
});
Mammal = Object.create(Chordate, {
has_hair: { value: true }
});
m = Object.create(Mammal);
m.init(‘dog’);
name has_hair has_spine init eats
m Mammal Chordateproto proto Animalproto
Exercise
Write the instanceof operator as if it was implemented in JS.
var Person = function(name) {
this.name = name;
}
person = new Person( 'Alexis' );
INSTANCEOF( person, Person );
Hints:
• Check the proto chain for the constructor’s prototype.

More Related Content

PPTX
Context
KEY
JavaScript Classes and Inheritance
PDF
Python - Lecture 4
PDF
WordCamp Portland 2018: PHP for WordPress
PPTX
Dr iterate
PDF
Python fundamentals - basic | WeiYuan
PPTX
Building a horizontally scalable API in php
ODP
Basic inheritance in JavaScript
Context
JavaScript Classes and Inheritance
Python - Lecture 4
WordCamp Portland 2018: PHP for WordPress
Dr iterate
Python fundamentals - basic | WeiYuan
Building a horizontally scalable API in php
Basic inheritance in JavaScript

What's hot (20)

PDF
Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...
PPTX
Switching from java to groovy
PDF
Thesis presentation Samuel Lampa
PDF
Purely functional data structures
PPTX
PDF
Chapter 1 python basics
PPTX
Grails queries
PDF
JRuby hacking guide
PDF
Useful javascript
PPTX
Learn python - for beginners - part-2
PDF
Fundamental JS
PDF
Functional Pattern Matching on Python
PPTX
Hands on Hadoop and pig
PDF
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
PDF
Functional Programming with Groovy
PPTX
Grails GORM - You Know SQL. You Know Queries. Here's GORM.
PPTX
Power shell
PDF
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...
Switching from java to groovy
Thesis presentation Samuel Lampa
Purely functional data structures
Chapter 1 python basics
Grails queries
JRuby hacking guide
Useful javascript
Learn python - for beginners - part-2
Fundamental JS
Functional Pattern Matching on Python
Hands on Hadoop and pig
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
Functional Programming with Groovy
Grails GORM - You Know SQL. You Know Queries. Here's GORM.
Power shell
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Ad

Viewers also liked (6)

PPTX
Closures
PPTX
Types, Operators and Primitives
PPTX
Comparisons
PPTX
Events - Part 2
PPTX
Element Styles and Positioning
PPTX
Basic JS
Closures
Types, Operators and Primitives
Comparisons
Events - Part 2
Element Styles and Positioning
Basic JS
Ad

Similar to Prototypes (20)

PDF
__proto__-and-prototype
PDF
FITC CoffeeScript 101
PDF
JavaScript Inheritance
PPTX
Ian 20150116 java script oop
PDF
JS OO and Closures
DOC
Jsphp 110312161301-phpapp02
PPT
Polymorphism.ppt
PPT
Oops implemetation material
PDF
Prototype
PPT
Class and Objects in PHP
PPT
10 classes
KEY
Javascript tid-bits
ODP
Learnjs
PPT
JavaScript Functions
PPTX
javascript prototype
PPT
Class
PPT
JSConf: All You Can Leet
PDF
Use the following code for the tasks public class Animal .pdf
PDF
Say It With Javascript
__proto__-and-prototype
FITC CoffeeScript 101
JavaScript Inheritance
Ian 20150116 java script oop
JS OO and Closures
Jsphp 110312161301-phpapp02
Polymorphism.ppt
Oops implemetation material
Prototype
Class and Objects in PHP
10 classes
Javascript tid-bits
Learnjs
JavaScript Functions
javascript prototype
Class
JSConf: All You Can Leet
Use the following code for the tasks public class Animal .pdf
Say It With Javascript

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
A Presentation on Artificial Intelligence
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Machine Learning_overview_presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
sap open course for s4hana steps from ECC to s4
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Electronic commerce courselecture one. Pdf
Empathic Computing: Creating Shared Understanding
Assigned Numbers - 2025 - Bluetooth® Document
A Presentation on Artificial Intelligence
Per capita expenditure prediction using model stacking based on satellite ima...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
A comparative analysis of optical character recognition models for extracting...
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.
MIND Revenue Release Quarter 2 2025 Press Release
MYSQL Presentation for SQL database connectivity
Machine Learning_overview_presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
sap open course for s4hana steps from ECC to s4
The Rise and Fall of 3GPP – Time for a Sabbatical?
20250228 LYD VKU AI Blended-Learning.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Spectral efficient network and resource selection model in 5G networks
Digital-Transformation-Roadmap-for-Companies.pptx
Electronic commerce courselecture one. Pdf

Prototypes

  • 1. JavaScript Training Goal Trainers Format • Lecture • Exercises • Ask Questions! • bitovi/js-training
  • 3. Shared Properties Animal = function(){ this.offspring = []; } Dog = function(){} Dog.prototype = new Animal() var dog1 = new Dog(), dog2 = new Dog(), pup = new Dog(); dog1.offspring.push(pup); dog2.offspring; // -> ?
  • 4. Prototype methods Animal = function(name) { this.name = name; } Animal.prototype.eats = function(){ return this.name + " is eating." } sponge = new Animal(“Bob”) name Bob __proto__ sponge prototype fneatsAnimal PrototypefnAnimal
  • 6. Proto-chaining Mammal.prototype.has_hair = true; Chordate.prototype = new Animal(); Animal = function(name) { this.name = name } Animal.prototype.eats = function(){ return this.name+" is eating." } Chordate = function(name){ this.name = name; } Chordate.prototype.has_spine = true; Mammal = function(name){ this.name = name; } Mammal.prototype = new Chordate(); m = new Mammal(‘dog’); m Object proto has_hair Mammal Chordat Animal Mammal Prototype Chordate Prototype Animal Prototype prototype has_spine eats prototype prototype proto proto Animal Object Chordate Object
  • 7. Proto-chaining Mammal.prototype.has_hair = true; Chordate.prototype = new Animal(); Animal = function(name) { this.name = name } Animal.prototype.eats = function(){ return this.name+" is eating." } Chordate = function(name){ Animal.call(this, name); } Chordate.prototype.has_spine = true; Mammal = function(name){ Chordate.call(this, name); } Mammal.prototype = new Chordate(); m = new Mammal(‘dog’); m Object proto has_hair Mammal Chordat Animal Mammal Prototype Chordate Prototype Animal Prototype prototype has_spine eats prototype prototype proto proto Animal Object Chordate Object
  • 8. Shared Properties Animal = function(){ this.offspring = []; } Dog = function(){} Dog.prototype = new Animal() var dog1 = new Dog(), dog2 = new Dog(), pup = new Dog(); dog1.offspring.push(pup); dog2.offspring; // -> pup Animal PROTOTYPE PROTOTYPE OFFSPRING dog1 dog2 pup Dog _proto_ _proto_ _proto_ _proto_
  • 9. Exercise Write the new operator as if it was implemented in JS. var Person = function(name) { this.name = name; } Person.prototype.speak = function(){ console.log(‘Hello!’) } var person = NEW( Person, ['name'] ); // var person = new Person(‘name’) person.speak(); Hints: • NEW takes the constructor function and an array of arguments
  • 10. Summary var Dog = function() {}; var pup = new Dog(); fn OBJECT OBJECT PROTOTYPE pup Dog
  • 11. Object.create Animal = { init: function(name) { this.name = name; }, eats: function() { return this.name + " is eating." } } Chordate = Object.create(Animal, { has_spine: { value: true } }); Mammal = Object.create(Chordate, { has_hair: { value: true } }); m = Object.create(Mammal); m.init(‘dog’); name has_hair has_spine init eats m Mammal Chordateproto proto Animalproto
  • 12. Exercise Write the instanceof operator as if it was implemented in JS. var Person = function(name) { this.name = name; } person = new Person( 'Alexis' ); INSTANCEOF( person, Person ); Hints: • Check the proto chain for the constructor’s prototype.

Editor's Notes

  • #11: Use previous example
  • #12: Tom: Summary slide; Animations should remain, but shapes/theme should match Justin’s
  • #13: Tom: Summary slide; Animations should remain, but shapes/theme should match Justin’s