how to make a object which has a simple method to add two digits in javascript
calc.sum(1,2);
here calc is an object with sum method which takes two parameters and return the value as 3.
the basic concept is
var calc = {};
calc.sum = function(a, b) {
return a+b;
}
As well you can do:
var createCalc = (function(){
var sum = function(a, b) {
return a+b;
};
return {
sum : sum
}
});
var cal = createCalc();
or
function Calc() {
this.sum = function(a, b) {
return a+b;
};
}
var calc = new Calc( );
I always recommend this github with patterns. http://shichuan.github.io/javascript-patterns/#object-creation-patterns