Published
Edited
Oct 19, 2018
1 star
Insert cell
Insert cell
{
const john = { name:"John" }
const joe = { pencil: "Joe's Pencil", name:"Joe" }
Object.setPrototypeOf(john, joe) // Joe is John's prototype
// John gets Joe's pencil. And he "returns it" to you.
return john.pencil
}
Insert cell
Insert cell
{
const john = { name:"John" }
const joe = { pencil: "Joe's Pencil", name:"Joe" }
Object.setPrototypeOf(john, joe)
return john.__proto__ // return's joe
}
Insert cell
Insert cell
// Set constructor function
Person = function( name ) {
this.name = name
}
Insert cell
// 2. Object creation
john = new Person("John")
Insert cell
Insert cell
{
// 1. Make a new empty Object.
const obj = {}
// 2. Set new object as "this" for the function
Person.call(obj, "Joe")
// 3. return the object
return obj
}
Insert cell
Insert cell
Person.prototype = {
introduce: function() { return "Hi I'm " + this.name + "." },
species: "human"
}
Insert cell
john.introduce()
Insert cell
Insert cell
john.__proto__ === Person.prototype
Insert cell
Insert cell
jim = {
// 1. Make a new empty Object.
const jim = {}
// 2. Set new object as "this" for the function
Person.call(jim, "Jim")
// 3. Set the __proto__ of the new object to prototype of class function
Object.setPrototypeOf(jim, Person.prototype)
// 4. return the object
return jim
}
Insert cell
Insert cell
class ES6Person {
constructor(name) {
this.name = name
this.species = "human"
}
introduce() {
return "Hi I'm " + this.name + "."
}
}
Insert cell
jack = new ES6Person("Jack")
Insert cell
jack.introduce()
Insert cell
Insert cell
jack.__proto__ === ES6Person.prototype
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