Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 何时对数组使用hasOwnProperty?_Actionscript 3_Flash - Fatal编程技术网

Actionscript 3 何时对数组使用hasOwnProperty?

Actionscript 3 何时对数组使用hasOwnProperty?,actionscript-3,flash,Actionscript 3,Flash,我刚刚看到一个示例,其中代码如下: var schemaSet:Array = currentScope(); if (schemaSet.hasOwnProperty("current")) schema = schemaSet["current"]; 我以前从未见过这种情况。我查看了文档,没有找到任何相关信息。注意:此代码来自Adobe员工编写的SchemaManager.currentSchema() 您将在何时何地使用此功能?这是否优于或等同于: if (schemaSet.i

我刚刚看到一个示例,其中代码如下:

var schemaSet:Array = currentScope();
if (schemaSet.hasOwnProperty("current"))
    schema = schemaSet["current"];
我以前从未见过这种情况。我查看了文档,没有找到任何相关信息。注意:此代码来自Adobe员工编写的SchemaManager.currentSchema()

您将在何时何地使用此功能?这是否优于或等同于:

if (schemaSet.indexOf("current")!=-1)

有两种类型的数据:

  • 普通数组,仅包含值,键为数字,从0开始

  • 还有一些对象,其中每个元素都有一个字符串(也可以是数字)键

如果要检查数组是否包含值,请使用.indexOf()方法

如果要检查对象是否有具有特定键的元素,则可以使用hasOwnProperty()方法


谢谢这就是我的想法,但我忘了提到代码示例来自Adobe员工编写的Adobe Flex SDK。因此,如果数组包含字符串,那么
hasOwnProperty()
indexOf()>-1
是相同的,实际上不是。indexOf()将只在值中查找,hasOwnProperty()将只在键中查找。因此,根据您要检查的内容,您将选择适当的方法。
var myArray = ["a", "b", "c"];

if (myArray.indexOf("a") > -1) return true;
var myObject = {a: "letter a", b: "letter b"}

if (myObject.hasOwnProperty("a")) return true;