Verify that property of AS3 SharedObject data exists

actionscript-3cookiesflashshared

How do I verify that certain properties of SharedObject exist? In other words, I want to make sure I read something sensible from my local SharedObject (one should not get anything good the first time loading swf )

I am going from information found on http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/SharedObject.html

Let's say I define

var my_so:SharedObject = SharedObject.getLocal("superfoo");

Now, if I have already gotten something under my_so.data.foobar I would like to use it in my application. (myapp.myfoo already has a default value before this)

if (my_so.data.foobar != null) myapp.myfoo=my_so.data.foobar;

This seems to hang on Flash 10.1 . What am I missing ?

EDIT: Thanks to your help the issue is fixed. 🙂

However, it would be nice to see the idiomatic way of doing this kind of check for SharedObject. I've found many examples teaching how to use SharedObject but they seem to gloss over the fact that you need to check whether you saved anything worthwhile in it. That is exactlyl what happens on loading .swf the first time. Another issue would be checking whether one successfully instantiated SharedObject (for example, when it is disabled), but that would be a whole another question…

Best Answer

I suspect you are running into the null vs undefined issue.

I would try using the hasOwnProperty method:

if (my_so.data.hasOwnProperty("foobar")) myapp.myfoo=my_so.data.foobar;

There are several methods for determining whether an object has a particular property, but hasOwnProperty() is probably the best way to do it.

Related Topic