Class with extends - Inheritance
<html><head>
<script>
class Animal
{
constructor(kind, name, size, weight, speed)
{
this.kind = kind;
this.name = name;
this.size = size;
this.weight = weight;
this.speed = speed + " mph";
}
eat()
{
console.log('eating');
}
saySpeed()
{
console.log(this.speed);
}
}
class Cat extends Animal
{
constructor(kind, name, size, weight, speed, tailSize)
{
super(kind, name, size, weight, speed);
this.tailSize = tailSize;
}
meow()
{
console.log('meowing');
}
sayKind()
{
console.log(this.kind);
}
tailWag()
{
console.log(this.tailSize + ' tail wagging');
}
}
var felix = new Cat(
'tiger', //kind
'Felix', //name
'Medium', //size
'10.0', //weight
'80', //speed
'long'); //length
//make felix use the custom functions
felix.meow();
felix.eat();
felix.sayKind();
felix.tailWag();
felix.saySpeed();
</script>
</head>
<body>
</body>
</html>
<script>
class Animal
{
constructor(kind, name, size, weight, speed)
{
this.kind = kind;
this.name = name;
this.size = size;
this.weight = weight;
this.speed = speed + " mph";
}
eat()
{
console.log('eating');
}
saySpeed()
{
console.log(this.speed);
}
}
class Cat extends Animal
{
constructor(kind, name, size, weight, speed, tailSize)
{
super(kind, name, size, weight, speed);
this.tailSize = tailSize;
}
meow()
{
console.log('meowing');
}
sayKind()
{
console.log(this.kind);
}
tailWag()
{
console.log(this.tailSize + ' tail wagging');
}
}
var felix = new Cat(
'tiger', //kind
'Felix', //name
'Medium', //size
'10.0', //weight
'80', //speed
'long'); //length
//make felix use the custom functions
felix.meow();
felix.eat();
felix.sayKind();
felix.tailWag();
felix.saySpeed();
</script>
</head>
<body>
</body>
</html>
Object Oriented Javascript
Audio File - Intro - Tutorial Teaching Resource