Created by
Classes inherits from classes!
var car = {};
var Car = function(){};
var ford = new Car();
var BMV = new Car();
var carFactory = function(){ return {} }
var ford = carFactory();
var BMV = carFactory();
function CarConstructor(){};
CarConstructor.prototype.sayHello=function(){
console.log(`Hello from CarConstructor.prototype`);
}
// creating objects with Constructor will set the "__proto__" to the Constructor.prototype
var constructedCar = new CarConstructor();
constructedCar.sayHello();
//Hello from CarConstructor.prototype
function CarFactory(){
return {}
};
CarFactory.prototype.sayHello=function(){
console.log(`Hello from CarFactory.prototype`);
}
// creating objects with factory will NOT set the "__proto__" to the Factory.prototype
var fabricatedCar = CarFactory();
fabricatedCar.sayHello();
//TypeError: fabricatedCar.sayHello is not a function
// create 'Car' constructor
var Car = function(manufacturer, speed){
this.manufacturer = manufacturer || undefined;
this.speed= speed || 100;
}
Car.prototype= {
drive: function(){
if (this.wings >= 2){
console.log(`Flying wiht ${this.speed} km/h`);
}else{
console.log(`Driving wiht ${this.speed} km/h`);
}
}
}
// create 'Ford' constructor
var Ford = function( speed){
var manufacturer = 'Ford';
// use the parent constructor
Car.apply(this, [manufacturer, speed]);
}
// set the prototype chain
Ford.prototype.__proto__ = Car.prototype;
// create 'ford' object
var ford = new Ford(200);
// create 'theFordOfPesho' object
var theFordOfPesho = new Ford(300);
theFordOfPesho.wings = 2;
ford.drive();
theFordOfPesho.drive();
// create 'car' object
var car = {
manufacturer: undefined,
speed: 100,
drive: function(){
if (this.wings >= 2){
console.log(`Flying wiht ${this.speed} km/h`);
}else{
console.log(`Driving wiht ${this.speed} km/h`);
}
}
}
// create the 'ford' object
var ford = {
// make 'ford' to inherit from 'car'
__proto__ : car,
manufacturer: "Ford",
speed: 200,
}
// create 'theFordOfPesho' object
var theFordOfPesho = {
// make 'theFordOfPesho' to inherit from 'ford'
__proto__ : ford,
wings: 2,
speed: 300,
}
ford.drive();
theFordOfPesho.drive();
__proto__
__proto__
outside of object literals!
// create 'car' object
var car = {
manufacturer: undefined,
speed: 100,
drive: function(){
if (this.wings >= 2){
console.log(`Flying wiht ${this.speed} km/h`);
}else{
console.log(`Driving wiht ${this.speed} km/h`);
}
}
}
// create 'ford' and make it to inherit from 'car'
var ford = Object.create( car, {
manufacturer: {value: "Ford"},
speed: {value: 200},
})
// create 'theFordOfPesho' and make it to inherit from 'ford'
var theFordOfPesho = Object.create(ford, {
wings: {value: 2},
speed: {value: 300},
})
ford.drive();
theFordOfPesho.drive();
__proto__
'Object.create()
aproach, instead of '__proto__
'
// Animal constructor
var Animal = function(food){
this.food = food;
}
Animal.prototype.eat = function(){
console.log(`${this.name} is eating ${this.food}`);
}
// Cat constructor
var Cat = function(name, food){
// reuse the Animal Constructor
Animal.call(this, food);
this.name = name || 'Tom';
}
// Cat objects will inherit from Animal.prototype
Cat.prototype = Object.create(Animal.prototype);
// Cat instance
var tom = new Cat('Tom', 'cheese');
tom.eat();
name
: stringage
: numbersgreet
: function, which prints out 'Hi, I'm ${this.name} and I'm ${this.age} years old'name
: stringage
: numbersskillset
: array of a developer skillsetsname
: stringage
: numbersmanaged
: array of developers being managedgreet
method!You can use the next code template to bootstrap your solution:
/* --------------------------- Person constructor --------------------------- */
function Person(name, age){
// your code here ...
}
// Person greet method
// your code here ...
/* --------------------------- Manager constructor -------------------------- */
function Manager(name, age, managed){
// reuse Person constructor
// your code here ...
}
// Manager objects should inherit all methods from Person:
/* -------------------------- Developer constructor ------------------------- */
function Developer(name, age, skillset){
// reuse Person constructor
// your code here ...
}
// Developer objects should inherit all methods from Person:
/* ----------------------------- Create Objects ----------------------------- */
// Developer instances
let maria = new Developer('Maria Popova', 23, ['Python', 'Machine Learning']);
let pesho = new Developer('Petar Petrov', 19, ['JavaScript', 'Angular', 'React', 'Vue']);
// Manager instances
let gates = new Manager('Bill Gates', 43, [maria, pesho]);
/* ----------------------------- Use the objects ---------------------------- */
maria.greet();
pesho.greet();
gates.greet();
Hi, I'm Maria Popova and I'm 23 years old
Hi, I'm Petar Petrov and I'm 19 years old
Hi, I'm Bill Gates and I'm 43 years old
greet()
method for Developer and Manager objects, such that the same code execution as given in the previous task template will produce next output:
Hi, I'm Maria Popova and I'm 23 years old
I know Python,Machine Learning
Hi, I'm Petar Petrov and I'm 19 years old
I know JavaScript,Angular,React,Vue
Hi, I'm Bill Gates and I'm 43 years old
I manage Maria Popova,Petar Petrov,
These slides are based on
customised version of
framework