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
}

5 Answers 5

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
}
18
if ("readable" in child) {
  ...
2
  • Is there any difference/downside to using "in" instead of "hasOwnProperty"?
    – OMA
    Oct 3, 2018 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 3, 2018 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) {
    ...
}
0

Try something like this:

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

}
2
  • 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, 2013 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, 2014 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.