28

I'm attempting to read a property on a series of Sprites. This property may or may not be present on these objects, and may not even be declared, worse than being null.

My code is:

if (child["readable"] == true){
    // this Sprite is activated for reading
}

And so Flash shows me:

Error #1069: Property selectable not found on flash.display.Sprite and there is no default value.

Is there a way to test if a property exists before reading its value?

Something like:

if (child.isProperty("readable") && child["readable"] == true){
    // this Sprite is activated for reading
}
Greg B
  • 14,597
  • 18
  • 87
  • 141
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607

5 Answers5

59

Objects in AS3 have the hasOwnProperty method which takes a string argument and returns true if the object has that property defined.

if(myObj.hasOwnProperty("someProperty"))
{
    // Do something
}
Greg B
  • 14,597
  • 18
  • 87
  • 141
18
if ("readable" in child) {
  ...
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Is there any difference/downside to using "in" instead of "hasOwnProperty"? – OMA Oct 03 '18 at 15:25
  • 1
    @OMA If `readable` is defined in `child`'s prototype instead of the instance itself, then `hasOwnProperty` will return `false` (e.g. `document.hasOwnProperty('getElementById') === false` while `('getElementById' in document) === true`) – kennytm Oct 03 '18 at 15:38
1

Adding this as it is a top response in Google.

If you are trying to check if a constant exists using a string for the name then use

if (ClassName["ConstName"] !== undefined) {
    ...
}
SystemicPlural
  • 5,629
  • 9
  • 49
  • 74
0

Try something like this:

if (child["readable"] != null){

}
smartali89
  • 209
  • 2
  • 8
  • 4
    this can cause errors if it doesn't exist in the first place. you would see the error if you are creating an object dynamically like looking for a["b"] in `var a:Object = {a:'1'}` – Daniel Mar 22 '13 at 18:16
  • var a ; a = a {a:1} ; trace(a["b"]) , outputs "undefined" , but does not generate any error. So, where is the problem in using this way ? – Vishwas Aug 16 '14 at 17:15
0

Response to @Vishwas G (not a comment because code blocks aren't supported in comments):

As Daniel pointed out, if the object "a" in your example doesn't exist in the first place, your attempt to access "b" on "a" will cause an error. This happens in cases where you're expecting a deep structure, such as a JSON object that might, for example, have the format "content.social.avatar". If "social" doesn't exist, then attempting to access "content.social.avatar" will cause an error.

Here's a general-case example of a deep-structure property-existence test where the "undefined" approach can cause an error in cases where the "hasOwnProperty()" approach does not:

// Missing property "c". This is the "invalid data" case.
var test1:Object = { a:{b:"hello"}};
// Has property "c". This is the "valid data" case.
var test2:Object = { a:{b:{c:"world"}}};

Now the tests...

// ** Error ** (Because "b" is a String, not a dynamic
// object, so ActionScript's type checker generates an error.)
trace(test1.a.b.c);  
// Outputs: world
trace(test2.a.b.c);  

// ** Error **. (Because although "b" exists, there's no "c" in "b".)
trace(test1.a && test1.a.b && test1.a.b.c);
// Outputs: world
trace(test2.a && test2.a.b && test2.a.b.c);  

// Outputs: false. (Notice, no error here. Compare with the previous
// misguided existence-test attempt, which generated an error.)
trace(test1.hasOwnProperty("a") && test1.a.hasOwnProperty("b") && test1.a.b.hasOwnProperty("c"));  
// Outputs: true
trace(test2.hasOwnProperty("a") && test2.a.hasOwnProperty("b") && test2.a.b.hasOwnProperty("c")); 

Note that ActionScript's sibling language JavaScript would not generate an error in the test1 example. However, if you extend the object hierarchy one more level, you'll bump into errors in JavaScript too:

// ** Error (even in JavaScript) ** because "c" doesn't even exist, so
// test1.a.b.c.d becomes an attempt to access a property on undefined,
// which always yields an error.
alert(test1.a.b.c.d)

// JavaScript: Uncaught TypeError: Cannot read property 'd' of undefined
colin moock
  • 1,038
  • 11
  • 15