JSFiddle - React, Tailwind, and code Playground
by Zaheer Ahmed
JavaScript
//Define human class
function Human(fullName) {
this.fullName = fullName;
}
// Add a couple of methods to Human.prototype
Human.prototype.speak = function(){
alert("I speak English!");
};
Human.prototype.introduction = function(){
alert("Hi, I am " + this.fullName);
};
//Define Student class
function Student(fullName, school, courses) {
Human.call(this, fullName);
// Initialize our Student properties
this.school = school;
this.courses = courses;
};
Student.prototype = Object.create(Human.prototype); // See note below
// Set the "constructor" property to refer to Student
Student.prototype.constructor = Student;
// override the "introduction" method
Student.prototype.introduction= function(){
alert("Hi, I am " + this.fullName + ". I am a student of " + this.school + ", I study "+ this.courses +".");
};
// Add a "exams" method
Student.prototype.takeExams = function(){
alert("This is my exams time!");
};
var student = new Student("Ahmed","NED University", "Computer Science");
student.introduction(); // "Hi, I am Ahmed. I am a student of NED University, I study Computer Science."
student.speak(); // "I speak English!"
student.takeExams(); // "This is my exams time!"
// Check that instanceof works correctly
alert(student instanceof Human); // true
alert(student instanceof Student); // true