21
loading...
This website collects cookies to deliver better user experience
class Bird {
constructor (name, age){
this.name=name;
this.age=age;
this.canFly= true;
}
}
const birdLarry= new Bird ("Larry", 4);
birdLarry.name;
to our console, the output would be "Larry". If we wrote birdLarry.age;
we'd get the number 4 as output. And if we wrote birdLarry.canFly
, the output would be "true". class Bird {
constructor (name, age){
this.name=name;
this.age=age;
this.canFly= true;
}
nameAge() {
return `${this.name} is ${this.age} years old`;
}
}
birdLarry.nameAge();
the output would be "Larry is 2 years old". Still, super simple. But what if we wanted to be able to change the name of our bird? Say, from Larry to Clumsy? and also change its age too? Because I feel like a bird named clumsy must be quite young. Let's add our methods:class Bird {
constructor (name, age){
this.name=name;
this.age=age;
this.canFly= true;
}
nameAge() {
return `${this.name} is ${this.age} years old`;
}
setName(name) {
this.name = name;
}
setAge(age) {
this.age = age;
}
}
birdLarry.setName("Clumsy");
and call birdLarry.name;
the output would be "Clumsy" since we modified the original name of our class instance. Also, since Clumsy must be a bit younger, we'd want to modify its age too by writing birdLarry.setAge(2)
into our console. Now, whenever we call birdLarry.age;
the out put would be 2. class Bird {
static kingdom(){
return "Animalia";
}
static kingdomSentence () {
return `Birds belong to the kingdom ${this.kingdom()}`;
}
constructor (name, age){
this.name=name;
this.age=age;
this.canFly= true;
}
}
birdLarry.kingdom()
, the output would be something like Uncaught TypeError: birdLarry.kingdom is not a function at <anonymous>:1:11
Uf. Why tho'?birdLarry
is an instance of the class Bird
. So, the static method wouldn't work with it. Instead, the static method would work only with the class Bird
. Like this:Bird.kingdom()
would return "Animalia"
. And if we wrote Bird.kingdomSentence()
it would return "Birds belong to the kingdom Animalia"
. Pretty cool eh?class Bird {
static kingdom= "Animalia";
static kingdomSentence=`Birds belong to the kingdom ${this.kingdom}`;
constructor (name, age){
this.name=name;
this.age=age;
this.canFly= true;
}
}
this.kingdom
. This way works just like the above, but it's cleaner. Now, what would happen if we wrote Bird.kingdom()
?Uncaught TypeError: Bird.kingdom is not a function at <anonymous>:1:6
, because now they're properties, not functions! So, we'd be calling them like : Bird.kingdom;
which would return "Animalia"
, and Bird.kingdomSentence
which would return "Birds belong to the kingdom Animalia"
.class Penguin extends Bird {
constructor (name,age){
super(name,age);
this.canFly=false;
}
}
const coldFeet= new Penguin("ColdFeet", 3);
coldFeet.age
, the output would be 3. If we wrote coldFeet.canFly
, the output would be false. Why? Obviously, because we made it clear that penguins can't fly :) What about other methods? Like, what would happen if we logged coldFeet.nameAge();
?"ColdFeet is 3 years old"
because, again, Penguin class shares everything with the Bird class!Penguin.kingdom
, the output would be "Animalia"
Penguin.kingdomSentence
, the output would be "Birds belong to the kingdom Animalia"
(because penguins are birds, silly!)21