The following is an example of a JavaScript program that demonstrates the use of classes:
In this example, a class called "Animal" is defined using the class
keyword, which has a constructor method that takes in two parameters, "name" and "type". These parameters are used to set the name and type properties of the Animal object. Additionally, a method called speak
is defined for the class, which outputs the name and type of the animal using the properties set by the constructor.
Then, two classes "Dog" and "Cat" are defined which inherit from the Animal class using the extends
keyword, and have a constructor that calls the super constructor method and sets a default value for the type property.
Finally, two instances of Dog and Cat class are created and the speak method is called on the instances.
You can implement this program in any JavaScript development environment that supports the use of classes, such as a text editor with a browser or Node.js.
No comments:
Post a Comment