Do you mean this: http://en.wikipedia.org/wiki/Objective-J (I'd never heard of it before just now) or do you just mean object oriented programming in JavaScript? (I'm assuming the latter.)
Anyway. I'm not 100% sure what you're trying to do here, but I'm assuming you probably want the option of calling calc.multiply with no arguments and retrieving the product of a and b, or passing arguments to calc.multiply and changing a and b in the process. In which case you could do this:
Code:
var calc = (function(){
var a;
var b;
return {
setOperands: function(x,y) {
a = x; b = y;
},
add: function(){
return a+b;
},
subtract: function(){
return a-b;
},
multiply: function(x,y){
if (!((x === undefined) || (y === undefined))) {
a = x; b = y;}
return a * b;
},
divide: function(){
if(b!=0)
return a/b
else{
alert('division by zero');
return false;
}
}
}
})();
console.info(calc.multiply()); // NaN
calc.setOperands(5,5);
console.info(calc.multiply()); // 25
console.info(calc.multiply(5,12)); // 60
console.info(calc.multiply()); // 60
Bookmarks