I’m going to be getting an array of objects and want to set instance variables inside of a class based on a property. So if I get this:
ary = [{type: 'walrus', name: 'GorbyPuff'}, {type: 'humanoid', occupation: 'KingSlayer'}]
I want to initialize an object where @walrus == ary[0] and @humanoid == ary[1]
In Ruby I could user instance_variable_set, but how can this be accomplished in the Javascripts?
I’m not sure if I get what you’re trying to acchieve, but the easiest way to do this would be:
The worry here is, that by altering the
aryvariable you will inadvertently alter thetheObj:If the
aryvariable is part of a function scope, and the resulting object is its return value, you needn’t worry. But if both are part of the global scope (Which they shouldn’t, it’s bad practice), this becomes an issue.I therefore propose this approach: