I'm setting up an object constructor and would like to pass an key/value object as a parameter. I was wondering what would be the correct way to do so? Right now I'm just passing the keys as the parameters:
(function() {
function Venue(name, poor, satisfactory, excellent) {
this.name = name;
this.serviceRatings = {
poor: poor,
satisfactory: satisfactory,
excellent: excellent
};
}
var dining = new Venue("dining", .1, .15, .2);
console.log(dining["serviceRatings"]["satisfactory"] // .15
}());
Although this works, coming from a Ruby background it only felt right to pass the object itself and assign the it's values in the initializer. This was the best equivalent I could think of:
class Venue
def initialize(name, serviceRatings = {})
@name = name
@poor = serviceRatings[:poor]
@satisfactory = serviceRatings[:satisfactory]
@excellent = serviceRatings[:excellent]
end
end
Is there a way to do this in Javascript? The way I have things written now makes me feel as if I'm bloating my constructer's list of parameters (unless this is the right way to do this).