1

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?

3 Answers 3

1

Arrays have nothing to do with it.

In Javascript, you can access object properties in one of two ways:

  1. theObject.theProperty
  2. theObject['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], x must be an array, because that's not the case at all.

A clarifying example follows:

var o = {
   'a': {
      'b': 5
   }
};

var x = 'a', y = 'b';

console.log(o.a.b);       // Output: 5
console.log(o['a']['b']); // Output: 5
console.log(o[x][y]);     // Output: 5

All of those three are equivalent, and o is still not an array.

Sign up to request clarification or add additional context in comments.

6 Comments

aah so actually this[m]=xx means this.m=xx as actortypes[id][m]=xx means actortypes[id].m=xx ? am i right?
@deniz: Almost. this.m is the same as this['m'], but instead you're getting this[m] which is the property whose name is the same as the contents of the variable m, not actually 'm'. There's an extra level of indirection.
for ex m = "asd", so i actually define this["asd"] (this."asd") = xx ?
@deniz: Essentially, yes. Except that this."asd" is not valid syntax, which is why this["asd"] exists instead.
Thanks. How does "for in" work for classes(i mean functions) ? Does it get ALL of the elements including functions ?
|
0

BaseActor and Actor are the methods attached to the Game object. The this inside Actor nothing but the reference to the object instance. So whenever a Game object will be instantiated, it will have these BaseActor and Actor methods attached to it. For e.g

var obj = new Game();

the return this.$.actorTypes[id] = new this.BaseActor(rate); inside the Actor will set the value of 'id' inside the actorTypes array object to the BaseActor object instance hence consisting of all the properties of that class.

Comments

0

Arrays and objects in Javascript are very similar. You can use for( var i in object ) to loop through members of an object, like you would loop through an array with for( var i = 0; i < array.length; i++ ). Similarly, you can use array access on an object: object.property is the same as object['property'].

6 Comments

Actually, arrays are rather just objects with numeric property names.
actually i have no problem about "for..in", i couldnt get this array and usage of actortypes
You can use for( var i in object ) to loop through members of an object as if they were an array. Except you should never loop through a Javascript Array in such a manner.
@LightnessRacesinOrbit No, you shouldn't. I didn't mean to imply that you should loop through arrays in that manner, just that you can loop through objects this way.
@RyanP: The "as if they were an array" suggests otherwise, but ok glad to hear it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.