Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test if a property exists on a object before reading its value?

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 } 
like image 990
Robin Rodricks Avatar asked May 02 '10 06:05

Robin Rodricks


People also ask

How do you check if a property exists in an object in Java?

Method 3: Check if a property exists in an object using the “in” operator. JavaScript offers a built-in “in” operator that determines whether the specified property belongs to an object or not. It returns “true” if the particular property exists in the object and “false” for the case when the property is not found.


2 Answers

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 } 
like image 133
Greg B Avatar answered Sep 19 '22 10:09

Greg B


if ("readable" in child) {   ... 
like image 41
kennytm Avatar answered Sep 22 '22 10:09

kennytm