Reputation: 9465
I would like to do something like the below code. Obviously this doesn't work as this does not allow you to pass in variable, which in the case below, is min and max.
Is it at all possible to do so? And if so, how?
Thanks. J.
var utils = (function() {
function randomRange(min, max) {
return ((Math.random() * (max - min)) + min);
};
return {
randomRange : randomRange(min, max);
};
}());
utils.randomRangeRound(20,5);
Upvotes: 0
Views: 65
Reputation: 114014
Alternatively, your code almost works you just got the syntax wrong:
var utils = (function() {
this.randomRange = function (min, max) {
return ((Math.random() * (max - min)) + min);
};
}());
Upvotes: 0
Reputation: 708056
If what you want is so that this will work:
utils.randomRangeRound(20,5);
then, you can do this:
var utils = {
randomRangeRound: function(min, max) {
return ((Math.random() * (max - min)) + min);
}
};
Or, if you have multiple methods on the utils
object assigned in different places in your code:
var utils = utils || {};
utils.randomRangeRound = function(min, max) {
return ((Math.random() * (max - min)) + min);
}
Upvotes: 3