OBJECT ORIENTED PROGRAMMING: INHERITANCE IN JAVASCRIPT · 2020-01-28 · OBJECT ORIENTED...

Post on 18-Jun-2020

16 views 0 download

transcript

Phachaya ChaiwchanLecturer in Information Science

Suan Sunandha Rajabhat University

Bangkok , Thailand

phachaya.ch@ssru.ac.th

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