Published
Edited
Jul 13, 2019
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function Doggo(name, vegetarian) { //the constructor can take in variables
this.species = 'doggo'
this.fluffy = true
this.talk = 'woooof'
this.name = name
this.vegetarian = vegetarian
//Doesn't have to return anything
}
Insert cell
Insert cell
//method:
Doggo.prototype.introduce = //Class.prototype is the object used to create the prototype of the object created (which we then refer to with Object.getPrototypeOf() or Object.setPrototypeOf())
function() {
if(this.vegetarian)
return 'Hai, my name is ' + this.name + ' and I am a vegetarian'
else
return 'Hai, my name is ' + this.name + ' and I am not a vegetarian'
}
Insert cell
Insert cell
clifford = new Doggo('Clifford', false)
Insert cell
clifford.introduce()
Insert cell
veggie = new Doggo('Veg', true)
Insert cell
veggie.introduce()
Insert cell
Insert cell
marvin = {
// 1. create empty object
var marvin = {}
// 2. run constructure with object as this
Doggo.call(marvin, "Marvin", false)
// 3. sets marvin's prototype to Bunny.prototype
Object.setPrototypeOf(marvin, Doggo.prototype)
/////// DONE ////////
return marvin
}
Insert cell
marvin.introduce()
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