Class Using return Methods and Inheritance
<html><head>
<title> Class Using return Methods and Inheritance</title>
<script>
class Person
{
constructor(firstName, lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
greet()
{
let theGreeting = "Howdy " +
this.firstName + " " + this.lastName;
return theGreeting;
}
bye()
{
let theSalutation = "Bye " +
this.firstName + " " + this.lastName;
return theSalutation;
}
}
class Alien extends Person
{
constructor(firstName, lastName, alienName)
{
super(firstName, lastName, alienName);
this.alienName = alienName;
}
alienGreeting()
{
let alienHi = "My name is not " +
this.firstName + " " +
this.lastName + ". " +
" My Real name is " +
this.alienName + ". " +
" I am an alien from another world.";
return alienHi;
}
}
function displayIt(where, whichOne) //DISPLAY
{
document.getElementById(where).innerHTML = whichOne;
}
let superman = new Alien("Clark","Kent", "Kal-El");
</script>
<body>
<button onclick="displayIt('div1', superman.greet())"> GREETING
</button>
<button onclick="displayIt('div2', superman.bye())"> SALUTATION </button>
<button onclick="displayIt('div3', superman.alienGreeting())">
ALIEN GREETING </button>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
</html>
<title> Class Using return Methods and Inheritance</title>
<script>
class Person
{
constructor(firstName, lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
greet()
{
let theGreeting = "Howdy " +
this.firstName + " " + this.lastName;
return theGreeting;
}
bye()
{
let theSalutation = "Bye " +
this.firstName + " " + this.lastName;
return theSalutation;
}
}
class Alien extends Person
{
constructor(firstName, lastName, alienName)
{
super(firstName, lastName, alienName);
this.alienName = alienName;
}
alienGreeting()
{
let alienHi = "My name is not " +
this.firstName + " " +
this.lastName + ". " +
" My Real name is " +
this.alienName + ". " +
" I am an alien from another world.";
return alienHi;
}
}
function displayIt(where, whichOne) //DISPLAY
{
document.getElementById(where).innerHTML = whichOne;
}
let superman = new Alien("Clark","Kent", "Kal-El");
</script>
<body>
<button onclick="displayIt('div1', superman.greet())"> GREETING
</button>
<button onclick="displayIt('div2', superman.bye())"> SALUTATION </button>
<button onclick="displayIt('div3', superman.alienGreeting())">
ALIEN GREETING </button>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
</html>