在ActionScript 3,什么是之间的差异和QUOT;而在"运营商和"的hasOwnProperty"方法? [英] In Actionscript 3, what is the difference between the "in" operator and the "hasOwnProperty" method?

查看:113
本文介绍了在ActionScript 3,什么是之间的差异和QUOT;而在"运营商和"的hasOwnProperty"方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在中经营者和的hasOwnProperty的方法似乎是可以互换的,但我想知道,如果一个检查继承的属性或东西,另一种是不或类似的东西。我在使用它了字典的情况下特别感兴趣,但我怀疑它是从其它用途不同。

The "in" operator and "hasOwnProperty" methods appear to be interchangeable, but I'm wondering if one is checking for inherited properties or something and the other isn't or something like that. I'm especially interested in the case of using it with a Dictionary, but I doubt that is different from other uses.

的hasOwnProperty在官方的文档中描述<一href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Object.html#hasOwnProperty%28%29">here和中被描述<一href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#in">here,但如果是有区别的,我并没有觉得很清楚。

"hasOwnProperty" is described in the official docs here and "in" is described here, but if there is a difference, I didn't find it very clear.

推荐答案

信任的preciously接受的答案居然让我到一点点麻烦。似乎有不只是原型相关的差异更多的事情。我所发现的是,

Trusting the preciously accepted answer actually got me into a little bit of trouble. There seems to be more going on than just prototype-related differences. What I've found is that

的hasOwnProperty不能用,看看是否有键按下present中时关键是引用类型的词典,但在操作员可以。

下面是一个例子来说明。

Here's an example to demonstrate.

code:

var test:Function = function(key:*,label:String):void
    {
        var d:Dictionary = new Dictionary(true);
        d[key] = true;
        trace(label);
        trace("  hasOwnProperty: " + (d.hasOwnProperty(key)?"true":"false <== !!PROBLEM!!"));
        trace("  in: " + (key in d));
        trace("  []: " + d[key]);
    };
test({}, "indexed by object");
test("string", "key is string");
test(0, "key is number");
test(true, "key is boolean");

结果:

indexed by object
  hasOwnProperty: false <== !!PROBLEM!!
  in: true
  []: true
key is string
  hasOwnProperty: true
  in: true
  []: true
key is number
  hasOwnProperty: true
  in: true
  []: true
key is boolean
  hasOwnProperty: true
  in: true
  []: true

这篇关于在ActionScript 3,什么是之间的差异和QUOT;而在&QUOT;运营商和&QUOT;的hasOwnProperty&QUOT;方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆