Inheritance in JavaScript
In this interactive lab, you will develop a basic understanding of classes, inheritance, and methods in JavaScript. The final outcome includes creating two classes, Person
and Student
, with the Student
class extending or inheriting from Person
. The lab will have you demonstrate JavaScript class syntax, methods, class constructors, and class inheritance concepts, creating a solid foundation in these areas.
Concepts
Few concepts you should familiarise yourself with before starting the lab are:
-
JavaScript Classes: A class is a type of function, defined with the
class
keyword. It's a blueprint for creating objects. -
Class Constructors: The constructor method is a special method for creating and initializing objects in a class.
-
JavaScript Methods: JavaScript methods are actions that can be performed on objects.
-
Class Inheritance: Class Inheritance is when a class inherits characteristics from another class.
Steps
To successfully complete this lab, you should perform the following steps:
-
Begin by creating a class named
Person
. This will be your parent class. -
Within the
Person
class, define a constructor that takesname
as a parameter. This constructor will set thename
property of your instances. -
Next, add a method to your
Person
class. This method,greeting
, should return a string that uses thename
property. -
Now, create a second class,
Student
, that inherits fromPerson
(i.e., it should extend thePerson
class). -
Within the
Student
class, define a constructor that takes two parameters:name
andcourse
. Make sure that your constructor calls thesuper()
method withname
as its argument. This will ensure that yourStudent
instances have aname
property. -
Lastly, add a method to the
Student
class namedcourseDetails
. This method should return a string that includes thecourse
property.
Examples
Here's a simplified example of a class with a method and a constructor:
class Sample { constructor(property) { this.property = property; } method() { return `This is my ${this.property}`; } }
In this example, Sample
is a class. constructor(property)
is a constructor method which sets the property
of the class, and method()
is a class method that utilizes this property.
Hints
-
To create a class in JavaScript, use the
class
keyword followed by the name of the class. -
The constructor method is a special method for creating and initializing an object created with a class.
-
super()
is used in the context of constructors to call a parent class's constructor. -
Methods in JavaScript classes are defined without the
function
keyword. -
Remember to use the
this
keyword when referring to class properties in methods.