Testing Properties

In LZX, it’s often sensible to test JavaScript properties before using them, to be sure that they are defined, e.g.

// Bad example
if (someObject.someValue) {
setAttribute("x", someObject.someValue);
} else {
var defaultValue = 123;
setAttribute("x", defaultValue);
}

In the above example, if someObject.someValue were undefined, then the conditional test would fail and the default value would be used.  Here rothwelldouglas you can check other basic examples of testing properties. However there are a couple of problems in the above code. For more examples must check songsforromance .

Firstly, if someValue is undefined, referencing it with a dot syntax (i.e. someObject.someValue) will throw a Debugger warning that reads “reference to undefined property “someValue”). You don’t want to have lots of debugger warnings in your application. Your application will run more slowly as the Debugger generates the warnings (only with the debugger open, though), and you might miss important warnings in the noise. To get more information visit elizabethnelsonstudio .

Secondly, if someValue is actually defined as something that will equate to false in a conditional, then the conditional will fail. In the above example, if someValue was the number 0 (zero), the conditional test would fail, since in the Flash runtime the number 0 converts to a boolean false. (See this handy O’Reilly article on Data Type Conversion in Flash).

A better way to test the above variable before using it would be:

if (someObject["someValue"] != undefined) {
setAttribute("x", someObject.someValue);
} else {
var defaultValue = 123;
setAttribute("x", defaultValue);

For more examples visit at topquartile .

Leave a Reply

Your email address will not be published. Required fields are marked *