Please login to start chat

ICT Solutions 22 Jun 2022, 08:01:10
Category : JavaScript Tutorials

Class Methods (JS Classes)

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Code Testing</title>
</head>
<body>
<p id = "demo">What is your name and age?</p>
<button type = "button" onclick = "jsFun()">Check Name and Age</button>
<script>
function jsFun(){
class Name{
constructor(Name, Year){
this.name = Name;
this.year = Year;
}

age(){
const date = new Date();
return date.getFullYear() - this.year;
}
}

const myName = new Name("Thet", 1987);
document.getElementById("demo").innerHTML = "My name is " + myName.name + " and my age is " + myName.age() + " years old now.";
}
</script>
</body>
</html>

Comments:

No comment to show.

Available Services
Download Android Application