PDA

View Full Version : What is this?



Crungmungus
May 11th, 2008, 09:43 PM
I came across this code while I was looking at an AS3 application:



_config = config;
var sprites:Object = {
L:config.tetradL,
O:config.tetradO,
F:config.tetradF,
T:config.tetradT,
S:config.tetradS,
Z:config.tetradZ,
I:config.tetradI
};


What is the terminology for creating a class in this fashion? I've not been able to find any resources which explain it's use and so on.

Any help appreciated,

Crung

Krilnon
May 11th, 2008, 10:52 PM
Using {} in that context means that you are using the object literal syntax (I've also seen it called the object initializer). Basically, you can define an object and its properties using key:value pairs. In the example that you posted, sprites.L would reference the same object that config.tetradL references. (Or, if tetradL is a primitive value, like 4, then L would be 4 as well.)

Rezmason
May 12th, 2008, 12:11 AM
Yes. It's the same as saying,
_config = config;
var sprites:Object = new Object;
sprites.L = config.tetradL;
sprites.O = config.tetradO;
sprites.F = config.tetradF;
sprites.T = config.tetradT;
sprites.S = config.tetradS;
sprites.Z = config.tetradZ;
sprites.I = config.tetradI; except that it's more truncated. By using those brackets, the value of the variable "sprites" is assigned a literal Object.