理解和使用hasOwnProperty

时间:2014-11-06 23:37:20

标签: actionscript-3

有人可以向我解释如何分配和使用hasOwnProperty。我确实在网上搜索了一个不错的例子,但不知何故甚至没有找到任何东西(或者我可以“聪明地”来理解它的含义)

所以我想做的就是将一个属性设置到一个MovieClip上,然后再查看它是否存在。

    var myMC:MovieClip = new MovieClip();
myMC.hasOwnProperty( "someRandomText" );

this.addChild( myMC );
if( myMC.hasOwnProperty( "someRandomText" ) ) trace(" yes it has it ")
else trace( "nothing here" )

输出:nothing here

我做错了什么?

以及:)我将它添加到MC后如何将其删除/删除

1 个答案:

答案 0 :(得分:2)

hasOwnProperty()检查对象是否具有该名称的属性。基本上,如果属性具有与字符串匹配的实例名称,它将返回true。

hasOwnProperty("someRandomText")在代码中返回false的原因仅仅是因为myMC.someRandomText不存在。你的第二行似乎试图制作它,但这不是函数的作用。

更好的测试是:

if( myMC.hasOwnProperty( "width" ) ) trace(" yes it has it ");
else trace( "nothing here" );

所有MovieClip都有width属性,所以这应该返回true。我没有测试过,但它应该可以工作。

The definition on the AS3 Reference几乎是你能得到的最佳解释。