Question
In this sample code, there is a Person object and from the Person object, a Student object is derived. As we can see in this
In this sample code, there is a Person object and from the Person object, a Student object is derived.
As we can see in this example, the initialize method belongs to Person and the learn method belongs to Student, both of which are now part of the me object.
Keep in mind that there are many ways of doing inheritance in JavaScript, and this is just one of them.
Exercise Create an object called Teacher derived from the Person class, and implement a method called teach which receives a string called subject, and prints out:
[teacher's name] is now teaching [subject] JavaScript Programming
var Person = function() {}; Person.prototype.initialize = function(name, age) { this.name = name; this.age = age; } Person.prototype.describe = function() { return this.name + ", " + this.age + " years old."; } var Student = function() {}; Student.prototype = new Person(); Student.prototype.learn = function(subject) { console.log(this.name + " just learned " + subject); } var me = new Student(); me.initialize("John", 25); me.learn("Inheritance");
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started