Skip to content

Instantly share code, notes, and snippets.

@vibhanshuc
Last active February 13, 2016 11:23
Show Gist options
  • Save vibhanshuc/0bf0ecaa290f47499431 to your computer and use it in GitHub Desktop.
Save vibhanshuc/0bf0ecaa290f47499431 to your computer and use it in GitHub Desktop.
Defines a superclass named Person and subclass named Studenusing prototype chain
(function () {
'use strict';
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
Person.prototype.getName = function () {
return 'Name: ' + this.name.toString();
};
Person.prototype.getAge = function () {
return 'Age: ' + this.age.toString();
};
Person.prototype.getGender = function () {
return 'Gender: ' + this.gender.toString();
};
Person.prototype.getSummary = function () {
return [this.getName(),
this.getGender(),
this.getAge()].join(',\n');
};
function Student(name, age, sex, grade, school) {
Person.call(this, name, age, sex);
this.grade = grade;
this.school = school;
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.getGrade = function () {
return 'Grade: ' + this.grade.toString();
};
Student.prototype.getSchool = function () {
return 'School: ' + this.school.toString();
};
Student.prototype.getSummary = function () {
return [this.getName(),
this.getGender(),
this.getAge(),
this.getGrade(),
this.getSchool()].join(',\n');
};
var person = new Person('Sachin Tendulkar',
42,
'Male');
person.getSummary();
//Name: Sachin Tendulkar,
//Gender: Male,
//Age: 42
var student = new Student('Arjun Tendular',
16,
'Male',
12,
'Shardahram School');
student.getSummary();
// Name: Arjun Tendular,
// Gender: Male,
// Age: 16, Grade: 12,
// School: Shardahram School
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment