JSFiddle - React, Tailwind, and code Playground

by natarajgandhi

JavaScript

//Prototype pattern for the Calculator sample

//1. Create the constructor function and set the private properties for the object
var Calculator = function(a, b) {
	this.operand1 = a;
  this.operand2 = b;
};
//2. Create the proto object and assign all the public properties (API)

Calculator.prototype = {
	add: function() {
  	alert(this.operand1 + this.operand2);
  },
  subtract: function(){
  	alert(this.operand1 - this.operand2);
  }
}

var calci = new Calculator(5,2);
calci.add();
calci.subtract();

//override add method

Calculator.prototype.add = function(){
	alert(this.operand1 + this.operand2 + 55); 
}
calci.add();