February 18th 2018
Hello! At the ES6 course by Wes Bos I learned about classes. Before classes we used prototype inheritance, which when you put a method on the original constructor.
function Dog(name, breed) {
this.name = name;
this.breed = breed;
}
Dog.prototype.bark = function() {
console.log(`Bark Bark! My name is ${this.name}`)
}
If you write just snickers in the console you won't get the prototype.bark or snuggle because they are only instances of the prototype. But if you open the object they are there.
Two ways to make classes in ES.
Class declaration and class expression, two ways of making a Class.
Declaration class Dog {}
Expression const Dog = {}
class Dog {
constructor(name, breed) {
this.name = name;
this.breed = breed;
}
bark() {
console.log(`Bark Bark! My name is ${this.name}`);
}
extends a Class
class Dog extends Animal {
constructor(name, breed) {
super(name);
this.breed = breed;
}
name is already defined in an 'Animal' class.
Cheers!
nice writing, breed! breed!! breed!!!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks! Breedisgood!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit