I have seen a piece of code that I can’t figure out:
for (var m in client.actorTypes[d[5]]) {
if (m !== 'update' && m !== 'destroy' && m !== 'remove') {
this[m] = client.actorTypes[d[5]][m];
}
}
but actorTypes is not a 2D array:
Game.prototype.BaseActor = function(rate) {
this.updateRate = rate;
this.onCreate = function(data, complete) {};
this.onUpdate = function(data) {};
this.onEvent = function(data) {};
this.onInterleave = function() {};
this.onDraw = function() {};
this.onDestroy = function(complete) {};
};
Game.prototype.Actor = function(id, rate) {
return this.$.actorTypes[id] = new this.BaseActor(rate);
};
I actually don’t know what happens in this code. Can someone explain it to me? What is a this array, and how could actorTypes become a 2d array?
Arrays have nothing to do with it.
In Javascript, you can access object properties in one of two ways:
theObject.thePropertytheObject['theProperty']Method 1 is only possible with a literal, valid variable name; method 2 allows you to use an arbitrary expression (e.g. in your case, a string variable).
Arrays are a special case of Javascript objects, which happen to have numerically-named properties. We use method 2 to access them because valid variable names may not start with (or be solely) numbers.
That doesn’t mean that every time you see
x[y],xmust be an array, because that’s not the case at all.A clarifying example follows:
All of those three are equivalent, and
ois still not an array.