Published
Edited
Oct 30, 2019
Insert cell
Insert cell
{
var animal = {
alive: true,
canGrow: true,
}
// How we did it last class
var dog = {
species: "dog"
}
Object.setPrototypeOf(dog, animal)
// How to use Object.create
var cat = Object.create(animal) // --> {}
cat.species = "cat"
// animal is prototype of both
return cat.__proto__ === dog.__proto__
}
Insert cell
Insert cell
{
// storing a value into a new Object
var myObj = {}
myObj.myAttr = "my value"
// we can do the same with a function
var myFun = function() { }
myFun.myAttr = "my value"
// values can be retrieved from Functions just like Objects
myFun.myAttr
// the function attribute and object attribute are equal
return myObj.myAttr === myFun.myAttr
}
Insert cell
Insert cell
Bunny = {
function Bunny(name, age) {

// Do what you need to set up the object.

this.species = "bunny"
this.fluffy = true
this.age = age
this.name = name
this.sound = "Kyuu"
// doesn't need to return anything
}
// Add methods
Bunny.prototype.speak = function() {
return this.sound
}
Bunny.prototype.introduce = function() {
return "Hello, I'm " + this.name + ". I'm " + this.age + " years old."
}
return Bunny
}
Insert cell
Insert cell
phil = new Bunny("Phil", 2)
Insert cell
phil.introduce()
Insert cell
bob = new Bunny("Bob", 3)
Insert cell
bob.introduce()
Insert cell
Insert cell
marvin = {
// 1. create empty object
var marvin = {}
// 2. run constructure with object as this
Bunny.call(marvin, "Marvin", 3)
// 3. set marvin.__proto__ to Bunny.prototype
Object.setPrototypeOf(marvin, Bunny.prototype)
/////// DONE ////////
return marvin
}
Insert cell
marvin.introduce()
Insert cell
Insert cell
{
var myObj = {
foo: "bar"
}
// Notice this is not within an object. If I run myFun() what is `this`?
var myFun = function(param1, param2) {
return param1 + param2 + this.foo
}
var myBoundFun = myFun.bind(myObj, "Hello ", "World ")
// the three ways of running myFun below are equivalent
return {
bind: myBoundFun(),
call: myFun.call(myObj, "Hello ", "World "),
apply: myFun.apply(myObj, [ "Hello " , "World " ])
}
}
Insert cell
Insert cell
Cat = {
function Cat() {
}
return Cat
}
Insert cell
{
var oscar = new Cat("Oscar", 3, "siamese")
if( Object.values(oscar).length === 0 ) return md`Your answer will be checked here. Please don't change the code below.`
expect(oscar.introduce()).to.equal("Hi I'm Oscar. I'm a 3 year old siamese cat with no owner.")
oscar.owner = "Oscar"
expect(oscar.introduce()).to.equal("Hi I'm Oscar. I'm Oscar's 3 year old siamese cat.")
return success()
}
Insert cell
Insert cell
Dice = {
function Dice() {

}
return Dice
}
Insert cell
{
var die = new Dice(1,10)
if( Object.values(die).length === 0 ) return md`Your answer will be checked here. Please don't change the code below`
var randomNumbers = Array(1000).fill(undefined).map( x => die.roll() )
for( var num of randomNumbers ) {
expect(num).to.be.at.least(1)
expect(num).to.be.at.most(10)
expect(num % 1).to.equal(0)
}
var uniqueRandomNumbers = new Set(randomNumbers)
expect(uniqueRandomNumbers.size).to.equal(10)
return success()
}
Insert cell
Insert cell
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