Skip to content

Instantly share code, notes, and snippets.

@RaDeleon
Created September 20, 2018 18:10
Show Gist options
  • Save RaDeleon/1e3c61518005c40f62784c57f0790c67 to your computer and use it in GitHub Desktop.
Save RaDeleon/1e3c61518005c40f62784c57f0790c67 to your computer and use it in GitHub Desktop.
FSW 14: Prototypes
// we built a prototype of Parent
function Parent(attributes) {
//console.log(attributes);
this.age = attributes.age;
this.name = attributes.name;
this.location = attributes.location;
this.phrase = attributes.phrase;
}
Parent.prototype.speak = function() {
console.log(`${this.name} says ${this.phrase}`)
}
function Child(childAttributes) {
//This binds the "this" keyword to Parent
Parent.call(this, childAttributes);
this.toy = childAttributes.toy;
}
// this sets up the __proto__ and allows us to use methods now across objects
Child.prototype = Object.create(Parent.prototype);
const fred = new Parent({
'age': 35,
'name': 'Fred',
'location': 'Bedrock',
'phrase': 'Yabba dabba DOOOO!',
'toy': 'Big Rock'
});
const willma = new Parent({
'age': 37,
'name': 'Willma',
'location': 'Bedrock',
'phrase': 'Fred!'
});
const pebbles = new Child({
'age': 2,
'name': 'Pebbles',
'location': 'Bedrock',
'phrase': 'Ma Ma',
'toy': 'rock doll'
});
console.log(pebbles.age);
console.log(willma.phrase);
fred.speak();
willma.speak();
pebbles.speak();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment