prototypes

12
JavaScript Training Goal Trainers Format Lecture Exercises Ask Questions! bitovi/js-trainin

Upload: alexisabril

Post on 17-Aug-2015

194 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Prototypes

JavaScript Training

Goal Trainers Format

• Lecture• Exercises• Ask Questions!• bitovi/js-training

Page 2: Prototypes

Prototypal Inheritance

Page 3: Prototypes

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

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

Prototypal Inheritance

Mammal

Chordate

Animal

MammalPrototype

ChordatePrototype

AnimalPrototype

prototype has_spinehas_hair eatsprototype prototype

proto proto

Page 6: Prototypes

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

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

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

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

Summary

var Dog = function() {};

var pup = new Dog();

fn

__proto__

OBJECT

OBJECT

PROTOTYPE

pup

Dog

Page 11: Prototypes

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

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.