07 - Resolving "this" (SJC)

by Diya_Khan

HTML

<div id="output"></div>

JavaScript

//Constructor with properties
var Calculator = function(elmId) {
    this.element = document.getElementById(elmId);
};

//Methods implementation with prototype
Calculator.prototype =function() {
        var addValues =function(x, y) {
            var sum = x + y;
            this.element.innerHTML = x + "+" + y + "=" + sum;
        },
        add= function(x, y) {
            //addValues(x,y);
            //Resolving this problem
            addValues.call(this,x,y);
        };
        return {
            add: add
        };
   
}();

//Creating object
var calc = new Calculator('output');
//Call method of object
calc.add(5, 3);