JavaScript InheritanceThe JavaScript inheritance is a mechanism that allows us to create new classes on the basis of already existing classes. It provides flexibility to the child class to reuse the methods and variables of a parent class. The JavaScript extends keyword is used to create a child class on the basis of a parent class. It facilitates child class to acquire all the properties and behavior of its parent class. Points to remember
JavaScript extends Example: inbuilt objectIn this example, we extends Date object to display today's date. Test it NowOutput: Current date: 31-8-2018 Let's see one more example to display the year value from the given date. Test it NowOutput: Year value: 1947 JavaScript extends Example: Custom classIn this example, we declare sub-class that extends the properties of its parent class. Test it NowOutput: Honda Shine 70000 JavaScript extends Example: a Prototype-based approachHere, we perform prototype-based inheritance. In this approach, there is no need to use class and extends keywords. Test it NowOutput: Honda Shine 70000 Next TopicJS Polymorphism |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India