Published
Edited
Feb 25, 2019
Insert cell
Insert cell
{
var vehicle = {
hasMotor: true,
transportsHumans: true,
numWheels: undefined,
maxRiders: undefined,
}
/////////////////////////////
var motorcycle = {
numWheels: 2, // can override a property by the same name
maxRiders: 2,
}
var bicycle = {
hasMotor: false // can override a property by the same name
}
Object.setPrototypeOf(motorcycle, vehicle) // motorcycle.__proto__ is now equal to vehicle
Object.setPrototypeOf(bicycle, motorcycle) // bicycle.__proto__ is now equal to motorcycle
/////////////////////////////
var sudan = {
numWheels: 4,
maxRiders: 5,
hasTrunk: true,
}
Object.setPrototypeOf(sudan, vehicle) // sudan.__proto__ is now equal to vehicle
var chevyCruze2019 = Object.create(sudan) // gives us an empty object with a __proto__ attribute set to sudan
chevyCruze2019.make = "Chevy",
chevyCruze2019.model = "Cruze",
chevyCruze2019.year = 2019
}
Insert cell
Insert cell
Insert cell
{
var vehicle = {
position: 0
}
var car = {
drive: function() {
// `this` is bound to the car object
this.position = this.position + 1
}
}
Object.setPrototypeOf(car, vehicle)
car.drive()
///////////////////////////////////////////
function drive() {
// `this` is the global window object (not the bus)
this.position = this.position + 1
}
var brokenDownBus = {
position: 100
}
Object.setPrototypeOf(brokenDownBus, vehicle)
drive() // can't drive the bus
var busDriver = drive.bind(brokenDownBus) // makes a new function which will have `this` bound the our bus
busDriver() // can drive the bus
drive.call(brokenDownBus) // runs drive with `this` set to our bus once.
}
Insert cell
Insert cell
Insert cell
Human = {
// Defining the class
var Human = function() {
this.hasTwoEyes = true
this.hasTwoFeet = true
}
// Define Methods
Human.prototype.speak = function() {
return "Hello!"
}
return Human
}
Insert cell
bob = {
// how to create a new Human
var bob = {}
Human.call(bob)
Object.setPrototypeOf(bob, Human.prototype)
return bob
}
Insert cell
tyler = new Human()
Insert cell
bob.speak()
Insert cell
tyler.speak()
Insert cell
Insert cell
Programmer = {
var Programmer = function() {
Human.call(this)
this.occupation = "developer"
}
Programmer.prototype = Object.create(Human.prototype)
Programmer.prototype.constructor = Programmer
Programmer.prototype.speak = function() {
return Human.prototype.speak.call(this) + " I'm a programmer."
}
Programmer.prototype.program = function() {
return md`\`console.log("Hello World!")\``
}
return Programmer
}
Insert cell
Insert cell
chris.speak()
Insert cell
chris.program()
Insert cell
Insert cell
Insert cell
{
var globalBob = "bob"
function innerBob()
{
var sinnerBob = globalBob
return sinnerBob;
}
innerBob()
globalBob
return sinnerBob
}
Insert cell
Insert cell
Insert cell
{
!try to figure this out without reading the errors from observable
function atomBombCode(activate)
{
if(activate===true)
return null
function theft()
{
var laptop = {
secret: "encryptedSecretsHere",
password: function(pass) {if(pass==="Fluffykins21") return "un" + this.secret}
}
var errorBob = bob
var sovietThief = laptop
return sovietThief
}
var americanSpy = sovietThief
var happyTheClown = laptop
return [laptop,theft(),americanSpy, sovietThief]
}
var thatBritishDude = americanSpy
var bobbyNugs = atomBombCode(true)[1]
var monsieurSam = atomBombCode(false)[0]
var ivanTheTerrible = theft()
var falsyFakeFriend = atomBombCode(false)[1]
var theAmerican = atomBombCode(true)[2]
var theMartian = atomBombCode(false)[4]
var ussr = falsyFakeFriend
var whatsTheSecret = undefined //say which var contains it here
return whatsTheSecret
}
Insert cell
Insert cell
Insert cell
function Poodle(name, owner) {
var breed = "poodle" //this is private
var sound = "Woof" //this is private
function introduce() {
return "I'm a " + breed + " named " + name +
". My owner is " + owner + "."
}
function setSound(s) {
sound = s
}

return {
talk: function() {
return sound + "! " + introduce()
},
setSound: setSound
}
}
Insert cell
{
var alfred = Poodle("Alfred", "George")
var sput = Poodle("Sput", "Jack")
var lola = Poodle("Lola", "Matt")
return alfred.talk() + "\n" +
sput.talk() + "\n" +
lola.talk() + "\n"
}
Insert cell
Insert cell
Insert cell
function factoryFactory(product, numberofFactories){
return [undefined, undefined, undefined, undefined]
}
Insert cell
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