Inheritance #2

Using Constructor function and new keyword

by bhupendra negi

JavaScript

function UserCreator(name, score) {
  this.name = name;
  this.score = score;
}
UserCreator.prototype.sayName = function() {
  alert(`Hi , my name is ${this.name}`);
}
UserCreator.prototype.increment = function() {
 this.score++;
}

var user1 = new UserCreator("Shyam",34);
console.log(user1);

// Subclassing

function PaidUserCreator (name,score,accountBalance) {
// it will fill in details of object created in UserCreator
UserCreator.call(this,name,score)
this.accountBalance = accountBalance;
}

PaidUserCreator.prototype = Object.create(UserCreator.prototype);
PaidUserCreator.prototype.constructor = PaidUserCreator;


PaidUserCreator.prototype.increaseBalance = function () { this.accountBalance ++}

var puser1 = new PaidUserCreator("Admin Ram",23,121);
puser1.increaseBalance();
puser1.sayName();
console.log(puser1);