JavaScript pseudo classical inheritance pattern
JavaScript is not computer language based on class. So developers have used pseudo classical inheritance pattern to mimic inheritance of class.
This is example of pseudo classical inheritance pattern code.
var Animal = (function() {
function Animal(age, weight) {
this.age = age;
this.weight = weight;
}
Animal.prototype.eat = function() {
return 'eat';
}
Animal.prototype.move = function() {
return 'move';
}
return Animal;
}())
var Bird = (function() {
function Bird() {
Animal.apply(this, arguments)
}
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;
Bird.prototype.fly = function() {
return 'fly'
}
return Bird;
}())
var bird = new Bird(1, 5);
console.log(bird); //Bird { age: 1, weight: 5 }
console.log(bird.eat()) //eat
console.log(bird.move()) //move
console.log(bird.fly()). //fly

0 댓글