13
Phachaya Chaiwchan Lecturer in Information Science Suan Sunandha Rajabhat University Bangkok , Thailand [email protected] OBJECT ORIENTED PROGRAMMING: POLYMORPHISM and ABSTRACTION IN JAVASCRIPT

OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

Phachaya ChaiwchanLecturer in Information Science

Suan Sunandha Rajabhat University

Bangkok , Thailand

[email protected]

OBJECT ORIENTED PROGRAMMING:

POLYMORPHISM and ABSTRACTION

IN JAVASCRIPT

Page 2: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

POLYMORPHISM

Page 3: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

POLYMORPHISM

Page 4: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

EXAMPLE

POLYMORPHISM

function Person(age, weight) {

this.age = age; this.weight = weight;

this.getInfo = function() {

return "I am " + this.age + " years old " +

"and weighs " + this.weight +" kg.";

}

}

function Employee(age, weight, high) {

this.high = high;

this.age = age;

this.weight = weight;

this.getInfo = function() {

return "I am " + this.age + " years old " +

"and weighs " + this.weight +" kg. " +

"and high " + this.high + " cm.";

}

}

Employee.prototype = new Person();

Employee.prototype.constructor = Employee;// The argument, 'obj', can be of any kind

// which method, getInfo(), to be executed depend on the

object

// that 'obj' refer to.

function showInfo(obj) { document.write(obj.getInfo()

+ "<br>");

}

var person = new Person(25,60);

var employee = new Employee(42,80,170);

showInfo(person); showInfo(employee);

Page 5: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

ABSTRACTION

Page 6: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

ABSTRACTION

Page 7: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

ABSTRACTION

EXAMPLE

Page 8: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

ABSTRACTION

Page 9: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

EXAMPLE

Person

First name

Last name

Skills

Job

Job

Company

Position

Salary

Zara

David

Mike

Developer

Web

Designer

Page 10: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

ABSTRACTION

EXAMPLE

class Person {

constructor({firstName, lastName, job}) {

this.firstName = firstName;

this.lastName = lastName;

this.job = job;

this.skills = [];

Person._amount = Person._amount || 0;

Person._amount++;

}

static get amount() {

return Person._amount;

}

return Person._amount;

}

get fullName() {

return `${this.firstName} ${this.lastName}`;

}

set fullName(fN) {

if (/[A-Za-z]\s[A-Za-z]/.test(fN)) {

[this.firstName, this.lastName] = fN.split(' ');

} else {

throw Error('Bad fullname');

}

}

learn(skill) {

this.skills.push(skill);

}

}

Page 11: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

ABSTRACTION

EXAMPLE

class Job {

constructor(company, position, salary) {

this.company = company;

this.position = position;

this.salary = salary;

}

}

const david = new Person({

firstName: ‘David',

lastName: ‘Jone',

job: new Job('Youtube', 'developer', 200000)

});

const mike = new Person({

firstName: ‘Mike',

lastName: 'Smith',

job: new Job('ATP', ‘WebDesigner', 1000000)

});

david.fullName = ‘David Jone';

david.learn(‘MIS01');

mike.learn('programming');

david.learn(‘MIS02');

Page 12: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

ABSTRACTION

Page 13: OBJECT ORIENTED PROGRAMMING: …...Employee.prototype = new Person(); Employee.prototype.constructor = Employee; // The argument, 'obj', can be of any kind // which method, getInfo(),

Q & A