prototypes
TRANSCRIPT
![Page 1: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/1.jpg)
JavaScript Training
Goal Trainers Format
• Lecture• Exercises• Ask Questions!• bitovi/js-training
![Page 2: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/2.jpg)
Prototypal Inheritance
![Page 3: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/3.jpg)
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; // -> ?
![Page 4: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/4.jpg)
Prototype methods
Animal = function(name) { this.name = name;}
Animal.prototype.eats = function(){ return this.name + " is eating."}
sponge = new Animal(“Bob”)
nameBob
__proto__
sponge
prototype fneatsAnimalPrototypefnAnimal
![Page 5: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/5.jpg)
Prototypal Inheritance
Mammal
Chordate
Animal
MammalPrototype
ChordatePrototype
AnimalPrototype
prototype has_spinehas_hair eatsprototype prototype
proto proto
![Page 6: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/6.jpg)
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’);
mObject
proto
has_hair
Mammal
Chordate
Animal
MammalPrototype
ChordatePrototype
AnimalPrototype
prototype
has_spine eats
prototype prototype
proto protoAnimalObject
ChordateObject
![Page 7: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/7.jpg)
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’);
mObject
proto
has_hair
Mammal
Chordate
Animal
MammalPrototype
ChordatePrototype
AnimalPrototype
prototype
has_spine eats
prototype prototype
proto protoAnimalObject
ChordateObject
![Page 8: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/8.jpg)
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_
![Page 9: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/9.jpg)
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
![Page 10: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/10.jpg)
Summary
var Dog = function() {};
var pup = new Dog();
fn
__proto__
OBJECT
OBJECT
PROTOTYPE
pup
Dog
![Page 11: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/11.jpg)
Object.createAnimal = {
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
![Page 12: Prototypes](https://reader030.vdocument.in/reader030/viewer/2022032513/55d0e532bb61eb173b8b456a/html5/thumbnails/12.jpg)
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.