How to dynamically access an object?

I saved the property name of the object as a string. In PHP, if an object was named $objectand a property variable was named $key, it could be accessed with $object->$key. How can I dynamically access properties in ActionScript?

+3
source share
2 answers

Access to it can be obtained as follows:

var obj = {
    property1: 'this is a property',
    property2: 'this is another property'
}
var key = 'property2';

obj[key]; // 'this is another property'
+7
source

You can access the values ​​and properties of your object using either the point operator or the array access operator:

var myObject:Object = new Object();
myObject.propString = "I'm a String";
myObject.propNumber = 22;
myObject.propObject = {keyOne: "Key String", keyTwo: 23};

trace(myObject["propString"], myObject.propNumber);  //I'm a String 22
trace(myObject.propObject.keyOne, myObject.propObject["keyTwo"]); //Key String 23

the above variable myObject can also be written as follows:

var myObject:Object = {propString: "I'm a String", propNumber: 22, propObject: {keyOne: "Key String", keyTwo: 23}};
+2
source

Source: https://habr.com/ru/post/1787830/


All Articles