Published
Edited
Sep 27, 2019
Insert cell
Insert cell
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( var 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
class Dog extends Animal {
constructor(name, sex, breed) {
// run superclass constructor
super(name, "dog", sex)
this.breed = breed
}
introduce() {
// extend superclass method
return super.introduce() + " Woof woof!"
}
bark() {
return "woof"
}
}
Insert cell
Insert cell
chase = new Dog("Chase", "male", "chihuahua")
Insert cell
chase.introduce()
Insert cell
chase.bark()
Insert cell
chase.breed
Insert cell
Insert cell
{
class Robot {
constructor() {
// static property
Robot.totalUnits = Robot.totalUnits + 1 || 1
this.serialNo = Robot.totalUnits
}
introduce() {
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
clif = new Dog('Clif', 'male', 'Big Red')
Insert cell
clif instanceof Dog
Insert cell
clif instanceof Animal
Insert cell
Dog 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