object oriented programming: inheritance in javascript · 2020-01-28 · object oriented...
TRANSCRIPT
Phachaya ChaiwchanLecturer in Information Science
Suan Sunandha Rajabhat University
Bangkok , Thailand
OBJECT ORIENTED PROGRAMMING:
INHERITANCE IN JAVASCRIPT
Inheritance
Inheritance
EXAMPLE
Inheritance
EXAMPLE
Inheritance in JavaScript
EXAMPLE
function Person(firstName, lastName) {
this.FirstName = firstName || "unknown";
this.LastName = lastName || "unknown";
};
Person.prototype.getFullName = function () {
return this.FirstName + " " + this.LastName;
}
Inheritance in JavaScript
EXAMPLE
•
•
Inheritance in JavaScript
EXAMPLE
function Student(firstName, lastName, schoolName, grade) {
Person.call(this, firstName, lastName);
this.SchoolName = schoolName || "unknown";
this.Grade = grade || 0;
}
//Student.prototype = Person.prototype;
Student.prototype = new Person(); Student.prototype.constructor = Student;
Inheritance in JavaScript
•
•
•
•
Inheritance in JavaScript
EXAMPLE
function Person(firstName, lastName) {
this.FirstName = firstName || "unknown";this.LastName = lastName || "unknown";
} Person.prototype.getFullName = function () {
return this.FirstName + " " + this.LastName; } function Student(firstName, lastName, schoolName, grade) {
Person.call(this, firstName, lastName); this.SchoolName = schoolName || "unknown";this.Grade = grade || 0;
}
//Student.prototype = Person.prototype;Student.prototype = new Person(); Student.prototype.constructor = Student; varstd = new Student("James","Bond", "XYZ", 10); alert(std.getFullName()); // James Bondalert(std instanceof Student); // truealert(std instanceof Person); // true
Overriding method
•
•
Overload Constructor
Multiple inheritance
Mixins
Mixins and Traits
Traits
•
•
•
•
•
•
Q & A
Workshop