Reputation: 32823
I am writing modular javascript code. I wrote a basic calculator with two inputs and four buttons which performs arithmetic operations. When I run my it shows this error in console.
Uncaught ReferenceError: add is not defined
This happens for all buttons. How can I fix this and why it does not work?
Here is my code
Upvotes: 0
Views: 4566
Reputation: 140210
You are mixing up variables and object properties. Javascript never implicitly looks up object properties - it's always a variable lookup with the exception of global object and with
-statement.
So specify the object:
calculation: function(operator) {
if(operator == 'add')
return this.add(valone, valtwo);
else if(operator == 'sub')
return this.sub(valone, valtwo);
else if(operator == 'mult')
return this.mult(valone, valtwo);
else
return this.div(valone, valtwo);
}
Upvotes: 3