Published
Edited
Oct 30, 2019
Insert cell
Insert cell
Animal = {
var Animal = function(name, species, sex) {
this.species = species
this.sex = sex
this.name = name
this.stomach = []
}
Animal.prototype.eat = function(food) {
this.stomach.push(food)
}
Animal.prototype.poop = function() {
var randomAmount = Math.round(Math.random() * (this.stomach.length - 1))
var doggyBag = []
for( let i = 0; i < randomAmount; i++ ) {
doggyBag.push( this.stomach.pop() )
}
return doggyBag;
}
Animal.prototype.introduce = function() {
return "Hi, my name is " + this.name + ". I'm a " + this.sex + " " + this.species + "."
}
return Animal
}
Insert cell
Insert cell
{
var matt = new Animal("Matt", "Human", "male")
matt.eat("pizza")
matt.eat("chocolate")
matt.eat("beef")
matt.poop()
return matt.stomach
}
Insert cell
Insert cell
Dog = {
var Dog = function(name, sex, breed) {
// 1. call the constructor of the superclass
Animal.call(this, name, "dog", sex)
// 2. set up instance variables unique to the Dog class
this.breed = breed
}
// 3. set the animal prototype as the prototype of Dog
Dog.prototype = Object.create(Animal.prototype)
// 4. restore the constructor property
Dog.prototype.constructor = Dog
// 5. add methods unique to Dog
Dog.prototype.bark = function() {
return "woof"
}
return Dog
}
Insert cell
Insert cell
chase = new Dog("Chase", "male", "doberman")
Insert cell
chase.introduce()
Insert cell
chase.bark()
Insert cell
chase.breed
Insert cell
Insert cell
{
var Robot = function() {
// static property
Robot.totalUnits = Robot.totalUnits + 1 || 1
this.serialNo = Robot.totalUnits
}
Robot.prototype.introduce = function() {
return "Hello. I am unit " + this.serialNo + " of " + Robot.totalUnits
}
var robo1 = new Robot()
var robo2 = new Robot()
var robo3 = new Robot()
var robo4 = new Robot()
return robo1.introduce()
}
Insert cell
Insert cell
classes = {
function Animal() {
}
function Bear() {
Animal.call(this)
}
Bear.prototype = Object.create(Animal.prototype)
Bear.prototype.constructor = Bear
function Wolf() {
Animal.call(this)
}
Wolf.prototype = Object.create(Animal.prototype)
Wolf.prototype.constructor = Wolf
return { Animal, Bear, Wolf }
}
Insert cell
{
var { Animal, Bear } = classes // Ignore this line for now. We'll learn how this works in two weeks.
var smokey = new Bear()
return smokey instanceof Bear && smokey instanceof Animal
}
Insert cell
{
var { Animal, Wolf } = classes // Ignore this line for now. We'll learn how this works in two weeks.
return Wolf instanceof Animal
// why is this false?
}
Insert cell
Insert cell
Robot = {
function Robot() {
}
return Robot
}
Insert cell
{
var robo = new Robot()
if(Object.values(robo).length === 0) return md`Your answer will be checked here`
robo.walk()
robo.walk()
expect(robo.displacement).to.equal(2)
expect(robo.report()).to.equal("I have walked 2 units")
robo.walk()
expect(robo.displacement).to.equal(3)
expect(robo.report()).to.equal("I have walked 3 units")
return success()
}
Insert cell
Insert cell
FastRobot = {
function FastRobot() {
}
return FastRobot
}
Insert cell
{
var fRobo = new FastRobot(5)
if(Object.values(fRobo).length === 0) return md`Your answer will be checked here`
expect(fRobo).to.be.an.instanceof(FastRobot)
expect(fRobo).to.be.an.instanceof(Robot)
expect(fRobo).to.not.have.own.property("report")
expect(fRobo.speed).to.equal(5)
fRobo.walk()
expect(fRobo.report()).to.equal("I have walked 5 units")
fRobo.walk()
expect(fRobo.report()).to.equal("I have walked 10 units")
expect(fRobo.displacement).to.equal(10)
return success()
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more