Created by
A classic definition of Object Oriented Programming paradigm is the one given by Grady Booch (1991) Object-oriented design: With Applications. p. 35
A method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships
Note that JavaScript is a language which implements the Prototype-based OOP paradigm
Java | JavaScript |
---|---|
Classes | Functions |
Constructors | Functions |
Methods | Functions |
Tables excerpt from Douglas Crockford's "Classical Inheritance in JavaScript"
var car = {};
console.log('The car object: ');
console.dir(car);
The Object object (i.e. the object created by Object constructor) does not have prototype. You can think of it as the God of all objects.
__proto__
property__proto__
.__proto__
property is part of the standard!
var car = {};
console.dir(car.__proto__);
// Object
console.dir(car.__proto__.__proto__);
// null
prototype
propertyprototype
property.prototype
property will be discussed in next slides.__proto__
vs. prototype
__proto__
with prototype
properties__proto__
property.prototype
property
var car = {};
var Car = function(){};
var ford = new Car();
var BMW = new Car();
var carFactory = function(){ return {} }
var ford = carFactory();
var BMW = carFactory();
function CarConstructor(){};
CarConstructor.prototype.sayHello=()=>
console.log(`Hello from CarConstructor.prototype`);
function CarFactory(){ return {} };
CarFactory.prototype.sayHello=()=>
console.log(`Hello from CarFactory.prototype`);
var constructedCar = new CarConstructor();
var fabricatedCar = CarFactory();
constructedCar.sayHello();
//Hello from CarConstructor.prototype
fabricatedCar.sayHello();
//TypeError: fabricatedCar.sayHello is not a function
Object.create( prototype_object [, propertiesObject] );
prototype_object
- the prototype for the new object.propertiesObject
- an object, describing the properties for the newly created object. The syntax is like the props
object in Object.defineProperties() methodreturn
- new object with the specified prototype object and properties (if given ).
var car = {
sayHello:()=>console.log(`Hello, I'm car literal`),
};
var ford = Object.create(car);
ford.sayHello();
// Hello, I'm car literal
console.log(ford.__proto__ === car);
// true
var car = {
drive : function(){
console.log(`Driving with ${this.speed} km/h`);
},
speed: 100, // a default value for all objects created by "car"
};
var ford = Object.create(car, {
speed: {
value: 200,
writable: false,
}
});
ford.drive();
// let's try to change the speed
ford.speed = 300;
console.log(`ford.speed: ${ford.speed}`);
__proto__
property.)
// create 'car' object
const car = {
manufacturer: undefined,
speed: 100,
drive: function(){
if (this.wings >= 2){
console.log(`Flying with ${this.speed} km/h`);
}else{
console.log(`Driving with ${this.speed} km/h`);
}
}
}
// create the 'ford' object
const ford = {
manufacturer: "Ford",
speed: 200,
// make 'ford' to inherit from 'car'
__proto__ : car,
}
// create 'theFordOfPesho' object
const theFordOfPesho = {
wings: 2,
speed: 300,
// make 'theFordOfPesho' to inherit from 'ford'
__proto__ : ford,
}
car.drive();
ford.drive();
theFordOfPesho.drive();
__proto__
__proto__
outside of object literals!
// create 'car' object
const car = {
manufacturer: undefined,
speed: 100,
drive: function(){
if (this.wings >= 2){
console.log(`Flying with ${this.speed} km/h`);
}else{
console.log(`Driving with ${this.speed} km/h`);
}
}
}
// create 'ford' and make it to inherit from 'car'
const ford = Object.create( car, {
manufacturer: {value: "Ford"},
speed: {value: 200},
})
// create 'theFordOfPesho' and make it to inherit from 'ford'
const theFordOfPesho = Object.create(ford, {
wings: {value: 2},
speed: {value: 300},
})
car.drive();
ford.drive();
theFordOfPesho.drive();
__proto__
'Object.create()
aproach, instead of '__proto__
'
// define Car Constructor:
function Car() {
this.manufacturer = undefined;
this.speed = 100;
}
Car.prototype.drive = function () {
if (this.wings >= 2){
console.log(`Flying with ${this.speed} km/h`);
}else{
console.log(`Driving with ${this.speed} km/h`);
}
}
// define Ford Constructor:
function Ford(speed) {
this.manufacturer = "Ford";
this.speed = speed;
}
// Make Ford objects to inherit from Car.prototype:
Ford.prototype = Object.create(Car.prototype);
// create a Ford object
let ford = new Ford(200);
// create another Ford object, and attach wings to it:
let fordOfPesho = new Ford(300);
fordOfPesho.wings = 2;
// let's use the object:
ford.drive();
fordOfPesho.drive();
// define Car Class:
class Car{
constructor(speed){
this.manufacturer = undefined;
this.speed = speed;
}
//using shorter syntax for method definitions:
drive(){
if (this.wings >= 2) {
console.log(`Flying with ${this.speed} km/h`);
} else {
console.log(`Driving with ${this.speed} km/h`);
}
}
}
// define Ford Class, which will inherits from Car:
class Ford extends Car{
constructor(speed) {
// call the Parent Constructor first:
super(speed);
this.manufacturer = "Ford";
}
}
// create a Ford object
let ford = new Ford(200);
// create another Ford object, and attach wings to it:
let fordOfPesho = new Ford(300);
fordOfPesho.wings = 2;
// let's use the object:
ford.drive();
fordOfPesho.drive();
// Animal constructor
const Animal = function(name, food){
this.name = name;
this.food = food;
}
// Animal methods
Animal.prototype.eat = function(){
console.log(`${this.name} is eating ${this.food}`);
}
// Create a Cat Constructor, which will inherit all methods from Animal:
// <<<<< your code start here
const Cat = function (name, food) {
// reuse parent constructor
Animal.call(this,name,food);
}
// inherit all parent's methods
Cat.prototype = Object.create(Animal.prototype);
// >>>>> your code ends here
// create tom object:
const tom = new Cat('Tom', 'cheese');
// use tom:
tom.eat();
// expected output:
// Tom is eating cheese
These slides are based on
customised version of
framework