Java Reference
In-Depth Information
The following snippet of code performs the same actions using the bracket notation:
// Assigns 1.0 to alphaValue
var alphaValue = redColor["alpha"];
// Make the color semi-transparent
redColor["alpha"] = 0.5;
The "red value" and "black value" properties contain a space, so you can use only
the bracket notation to access them:
// Assigns 1.0 to redValue
var redValue = redColor["red value"];
// Assigns 0.8 to the "red value" property
redColor["red value"] = 0.8
When you use the bracket notation, you can use any expression that can be
converted to the property name as a string. The following snippet of code assigns the
value 0.8 to the "red Value" property in two ways:
var prop = "red value";
redColor[prop] = 0.8; // Using a variable
redColor["red" + " " + "value"] = 0.8; // Using an expression
Consider the following code that defines a point2D object with two properties
named x and y , and attempts to read a nonexisting property named z :
// Define a point2D object
var point2D = {x:10, y:-20};
// Try accessing x, y, and z properties
var x = point2D.x;
var y = point2D.y;
var z = point2D.z;
print("x = " + x + ", y =" + y + ", z = " + z);
x = 10, y =-20, z = undefined
 
Search WWH ::




Custom Search