Skip to content

Instantly share code, notes, and snippets.

@RaDeleon
Created September 20, 2018 18:11
Show Gist options
  • Save RaDeleon/3686e5585040304427f86053c06eb8c7 to your computer and use it in GitHub Desktop.
Save RaDeleon/3686e5585040304427f86053c06eb8c7 to your computer and use it in GitHub Desktop.
FSW 14: Class Syntax
// Classes are NOT hoisted
class Parent {
constructor(attributes) {
this.age = attributes.age;
this.name = attributes.name;
this.location = attributes.location;
this.phrase = attributes.phrase;
}
// Methods go here:
speak() {
console.log(`${this.name} says ${this.phrase}`)
}
}// end of Parent class
class Child extends Parent {
constructor(childAttributes) {
super(childAttributes);
this.toy = childAttributes.toy;
}
// Methods go here:
play() {
console.log(`${this.name} plays with a ${this.toy}`)
}
}// end of Child class
class GrandChild extends Child {
constructor(grandChildAttributes) {
super(grandChildAttributes);
this.babySupplies = grandChildAttributes.babySupplies;
}
// Methods go here:
cry() {
console.log(`${this.name} starts to cry`);
}
change() {
console.log(`${this.name} needs a new ${this.babySupplies}`)
}
}// end of GrandChild class
const fred = new Parent({
'age': 55,
'name': 'Fred',
'location': 'Bedrock',
'phrase': 'Yabba dabba DOOOO!',
'toy': 'Big Rock'
});
const willma = new Parent({
'age': 57,
'name': 'Willma',
'location': 'Bedrock',
'phrase': 'Fred!'
});
const pebbles = new Child({
'age': 27,
'name': 'Pebbles',
'location': 'Bedrock',
'phrase': 'Ma Ma',
'toy': 'rock doll'
});
const bambamjr = new GrandChild({
'age': 2,
'name': 'Bam Bam Jr.',
'location': 'Bedrock',
'phrase': 'BAM! BAM!',
'toy': 'rock bat',
'babySupplies': 'diaper'
});
console.log(pebbles.age);
console.log(willma.phrase);
console.log(fred.toy);
fred.speak();
willma.speak();
pebbles.speak();
pebbles.play();
bambamjr.speak();
bambamjr.play();
bambamjr.cry();
bambamjr.change();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment