17.5.删除共享对象中保存的数据

问题
我想删除共享对象中的某个属性值或者干脆删除整个共享对象
解决办法
使用delete删除共享对象的data属性中的值,或使用clear( )方法清除整个共享对象
讨论
删除共享对象中的数据是很简单的,但是要注意方法,在ActionScript中我们经常看到删除对象或数组只要赋值为
+展开
-ActionScript
null或undefined即可,但是对于共享对象这样做却不行:
// 试图删除共享对象example的someVariable 值
// 这语句编译通过,但是并不是我们所期望的
example.data.someVariable = null;

因为共享对象中null和undefined都是有效的值,因此上面的代码并没有删除someVariable属性,只不过设置为null而已,正确的方法是使用delete命令,如:
+展开
-ActionScript
// Remove someVariable from the example shared object.
delete example.data.someVariable;

clear( )方法删除整个共享对象,实际上就是删除硬盘中的.sol文件,看下面的代码:
+展开
-ActionScript
// Create a shared object and store some data in it
var example:SharedObject = SharedObject.getLocal( "example" );
example.data.someData = "a value";
// Displays: a value
trace( example.data.someData );
// Remove the shared object from the disk
example.clear( );
// Displays: undefined
trace( example.data.someData );

需要注意的地方是清除数据后,共享对象的引用仍然是有效的,这是还是可以重新添加数据进行保存。

加支付宝好友偷能量挖...


评论(0)网络
阅读(114)喜欢(0)flash/flex/fcs/AIR