/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Objective Javascript beginner

I’m new to Objective javascript even I had a good amount of experience in Javascript. How do I pass my parameters to the calc closure here?

var calc = (function(){
var a=5;
var b=0;
return {
add: function(){
return a+b;
},
subtract: function(){
return a-b;
},
multiply: function(){
return a*b;
},
divide: function(){
if(b!=0)
return a/b
else{
alert(‘division by zero’);
return false;
}
}
}
})();

console.log(calc.divide());

I want to pass the parameters to calc like (calc.multiply(10,20));

Thanks in advance..

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@daihuwsAug 31.2012 — 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.)

As far as closures go, this is the best documentation I've found on the subject: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Closures

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
[/CODE]


Was that what you had in mind, sort of?
Copy linkTweet thisAlerts:
@daveyerwinAug 31.2012 — [CODE]var calc = (function(){
var a;
var b;
return {
add: function(){
return a+b;
},
subtract: function(){
return a-b;
},
multiply: function(){
return a*b;
},
divide: function(){
if(b!=0)
return a/b
else{
alert('division by zero');
return false;
}
},
set operands(ops) {
a = Number(ops.split(",")[0]);
b = Number(ops.split(",")[1]);
}
}
})();
calc.operands = "3,2"
alert(calc.add())[/CODE]
Copy linkTweet thisAlerts:
@rnd_meAug 31.2012 — here a simple way to do about what you want:

[CODE]var calc = function(a,b) {
return {
add: function() {
return a + b;
},
subtract: function() {
return a - b;
},
multiply: function() {
return a * b;
},
divide: function() {
if (b != 0) return a / b
else {
alert('division by zero');
return false;
}
}
}
};

//console.log(calc.divide());
//I want to pass the parameters to calc like (calc.multiply(10,20));

calc(10,20).multiply();[/CODE]


otherwise, you'll have to mod each method to accept formal arguments...
Copy linkTweet thisAlerts:
@prasawdauthorSep 01.2012 — Thank you rnd_me. That is just the answer I wanted. Thank you. ?
×

Success!

Help @prasawd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...