
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to implement Polymorphism in JavaScript?
Polymorphism
Polymorphism is one of the tenets of Object Oriented Programming (OOP). It helps to design objects in such a way that the they can share or override any behavior with the specific provided objects. Polymorphism takes advantage of inheritance in order to make this happen.
In the following example child objects such as 'cricket' and 'tennis' have overridden the 'select' method called from parent object 'game' and returned a new string respectively as shown in the output. Whereas another child object 'football', instead of overriding the select method, shared(inherited) the method and displayed the parent string as shown in the output.
Example
<html> <body> <script> var game = function () {} game.prototype.select = function() { return " i love games and sports" } var cricket = function() {} cricket.prototype = Object.create(game.prototype); cricket.prototype.select = function() // overridden the select method to display { new string. return "i love cricket" } var tennis = function() {} tennis.prototype = Object.create(game.prototype); // overridden the select method to display new tennis.prototype.select = function() string { return "i love tennis" } var football = function() {} football.prototype = Object.create(game.prototype); // shared parent property var games = [new game(), new cricket(), new tennis(), new football()]; games.forEach(function(game){ document.write(game.select()); document.write("</br>"); }); </script> </body> </html>
Output
i love games and sports i love cricket i love tennis i love games and sports
- Related Articles
- How to implement basic Animation in JavaScript?
- How to implement insertion sort in JavaScript?
- How to implement merge sort in JavaScript?
- How to implement quick sort in JavaScript?
- How to implement asynchronous loop in JavaScript?
- Implement divide & conquer logic in JavaScript to implement QuickSort
- How to implement multiple input checkbox in vanilla JavaScript?
- Polymorphism in Java
- Polymorphism in Python
- Difference between compile-time polymorphism and runtime polymorphism
- Runtime Polymorphism in Java
- Polymorphism example in C++
- Explain Polymorphism in PHP.
- Program to implement Bucket Sort in JavaScript
- How to implement backtracking for a climbing stairs practice in JavaScript?

Advertisements