How To Use Inheritance Using JavaScript?


Share:
Published By: Admin, Published On: Nov 25, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to use inheritance example.We will check how to use inheritance. This is a short guide on use inheritance in javascript. Here you will learn how to use javascript inheritance. Let's get started with how to use inheritance in javascript.

Here i will give you many example how to use inheritance using javascript.

Example : 1
<!DOCTYPE html>
<html>
    <head>
        <title>How To Use Inheritance Using JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h3>How To Use Inheritance Using JavaScript? - MyWebtuts.com</h3>
        <script>
            class Temp extends Date {
                constructor(year) {
                    super(year);
                }
            }
            var a1 = new Temp("February 26, 2012 15:10:10");
            document.writeln("Year value:")
            document.writeln(a1.getFullYear());
        </script>
    </body>
</html>
Output :
Year value: 2012
Example : 2
<!DOCTYPE html>
<html>
    <head>
        <title>How To Use Inheritance Using JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h3>How To Use Inheritance Using JavaScript? - MyWebtuts.com</h3>
        <script>
            class Temp
            {
                constructor()
                {
                    this.subject="Laravel";
                }
            }
            class a1 extends Temp {
                constructor(name,version) {
                    super();
                    this.name=name;
                    this.version=version;
                } 
            }
            var a2 = new a1("Developer","8");
            document.writeln(a2.subject+" "+a2.name+" "+a2.version);
        </script>
    </body>
</html>
Output :
Laravel Developer 8

It will help you...

Share: