< FAQENTRY>数组和散列(关联数组) [英] <FAQENTRY> Array and hash (associative array)

查看:60
本文介绍了< FAQENTRY>数组和散列(关联数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者为什么我只是做了myArray [''item01''] =" Computers"但myArray.length是

显示0.真是嘿?

有一种新趋势可以处理数组和哈希,因为它们是一些

同一件事的变化。但它们根本不存在。


如果你正在做* array,那么你必须只使用整数值来表示

数组索引,因为它是因为ALGOL。


哈希(关联数组)在JavaScript中不存在作为单独的
编程实体。但每个对象都从Object()构造函数继承内部哈希函数

。在散列中,所有键都是CDATA字符串(即使

你为一个键提供了一个数字,在内部它被排序并被视为一个

字符串)。

现在有一些JavaScript细节:当Array扩展Object时,它也可以用作散列的
。所以你可以这样做:


var arr = new Array();

//添加到数组,arr.length == 1

arr [0] = 10;


//添加新属性(键/值对)

// arr.length是*不受影响!

arr [''foo''] =''JavaScript有时很有趣'';


表单值总是返回到字符串函数。

所以在类似
的情况下,
arr [myForm.myField.value] = 4; //说myForm.myField.value == 17

JavaScript无法确定你想从中得到什么:是否要想要添加名为17的新属性
或者你想要

来添加一个索引为17的数组元素。

要安全地使用* array *,你应该做类似的事情:


var arr = new Array();

....

var i = Math.parseInteger(myForm.myFie * ld.value, 10);

如果(i){arr [i] =数量;}


这是一次价值检查。

要跳过值检查,您可以通过在值+和+前面加上值
来进行运行时典型化。 (脚本会尝试将以下表达式转换为

数字):


app [+ myForm.myField.value] =数量;


后来:

for(i = 0; i< arr.length; i ++){

// check arr [i]

}

如果你想使用哈希(比如使用项目名称作为你的键),你最好使用通用的Object()构造函数
你有哈希免费从

继承的属性。


var hash = new Object();

hash [key] = someValue ;


后来:


for(key in hash){

// check hash [key]

Or why I just did myArray[''item01''] = "Computers" but myArray.length is
showing 0. What a hey?
There is a new trend to treat arrays and hashes as they were some
variations of the same thing. But they are not at all.

If you are doing *array", then you have to use only integer values for
array index, as it was since ALGOL.

Hash (Associative array) doesn''t exists in JavaScript as a separate
programming entity. But each object inherits internal hash mechanics
from Object() constructor. In hash all keys are CDATA strings (even if
you provide a number for a key, internally it''s sorted and treated as a
string).
Now some JavaScript specifics: as Array extends Object, it can be also
used as a hash. So you can do something like:

var arr = new Array();
// add to array, arr.length == 1
arr[0] = 10;

// add new property (key/value pair)
// arr.length is *not* affected !
arr[''foo''] = ''JavaScript is funny sometimes'';

Form values always returned to function as strings.
So in a situation like
arr[myForm.myField.value] = 4; // say myForm.myField.value == 17
JavaScript cannot determine what do you want from it: whether
you want to add new property called "17" or you want
to add an array element with index 17.

To work securely with *array* you should do something like:

var arr = new Array();
....
var i = Math.parseInteger(myForm.myFie*ld.value, 10);
if (i) {arr[i] = quantity;}

This is with a value check.
To skip on value check you can do runtime typisation by prefixing value
with "+" (script will try to convert the following expression into a
number):

app[+myForm.myField.value] = quantity;

Then later:
for (i=0; i<arr.length; i++) {
// check arr[i]
}
If you want to use hash (say using item names as your keys), you better
use the generic Object() constructor to have you hash free from
inherited properties.

var hash = new Object();
hash[key] = someValue;

Then later:

for (key in hash) {
// check hash[key]

推荐答案

VK写道:
或者为什么我只是做了myArray [''item01''] = "计算机与QUOT;但是
myArray.length正在显示0.真是嘿?


你的意思是: -


< URL: http://www.jibbering.com/faq/#FAQ4_39 >


- 并问自己为什么你希望添加(或赋值)一个对象的命名属性,以对其另一个

属性产生副作用。

有一种新趋势可以处理数组和哈希值,因为它们是同一事物的一些变体。但是他们根本不是。


没有趋势,个人向他们不完整的技术主题申请

不合适的术语一直很常见

理解(甚至发明自己的),并且这种行为常见于
导致其他人的混淆/误解。

如果你正在做* array" ,那么你必须只使用数组索引的整数值,就像ALGOL一样。


这取决于你如何定义数组索引。在ECMAScript''数组中

index''只是Array规范中的一个相关概念(ECMA

262第3版:第15.4节),特别是

数组的特殊内部[[Put]]方法(ECMA 262第3版:部分

15.4.5.1)。


''数组索引''是_string_(P),其中 - ToString(ToUint32(P))===

P - 。因此,一个正32位有符号整数的字符串表示(0

到((2到32的幂) - 1)。

哈希(关联数组)不在JavaScript中作为单独的编程实体存在。但是每个对象都从Object()构造函数继承内部的哈希函数。


Javascript不是一种基于类的语言,因此在谈论继承时

在此上下文中通常通过原型继承

链正在被审视。数组有自己的原型对象

但该对象有自己的原型,即Object.prototype

对象。因此,Arrays继承了未在Array.prototype对象上显式定义的Object的所有原型属性。但是Arrays实际上从Object.prototype继承的唯一

属性是 -

valueOf - 方法,所有其他属性都被
$ b上定义的属性所掩盖$ b Array.prototype。

所有对象,数组,对象,函数,正则表达式等都是本机ECMAScript对象的
实例。因此它们都具有本机ECMAScritp对象的

特性,但不是因为

继承,它们具有该对象的特性,因为它们是

_are_那个对象。本机

ECMAScript对象的一个​​主要特征是命名属性可以添加到该对象

并在运行时分配值。这就是''hashtable''和

''关联数组'' - 就像javascript的功能来自。


实例之间的显着差异一个本机ECMAScript

对象充当一个数组而一个不是数组

对象的默认内部[[Put]]方法被替换为a

特殊替代品,用于关注用于向对象属性写入

的属性名称是否属于''数组索引'',并且

另外作用于Array'的 - length - 属性,在一些

的情况下,如果它们的话。

在散列中所有键都是CDATA字符串


CDATA不是ECMAScript中的相关概念。字符串原语是16位Unicode代码点的
序列。

(即使你提供一个密钥的数字,内部
它已被分类和处理作为一个字符串)。


括号表示法属性访问器的算法总是在表达式

的评估结果中调用

内部ToString函数括号。这是与语言相关的,无论

是否对象都是数组,都会发生。

现在有些JavaScript细节:因为Array扩展了Object,
它可以是也用作哈希。


它可以用作''hash'',因为它_is_是一个原生的ECMAScript对象,

不是因为它''扩展''对象。 br />
所以你可以这样做:

var arr = new Array();
//添加到数组,arr.length == 1
arr [0] = 10;

//添加新属性(键/值对)
// arr.length是*不受影响!
arr [''foo ''] =''JavaScript有时很有趣'';


是的,你可以。

表格值总是以字符串形式返回。


胡言乱语!表单

控件的DOM表示的''value''属性通常是String类型。函数和返回值没有

进入它。

因此在
arr [myForm.myField.value] = 4; //说myForm.myField.value == 17
JavaScript无法确定你想从中得到什么:是否要添加名为17的新属性。或者你想要
添加一个索引为17的数组元素。


Javascript确切知道如何处理该赋值。它将一个

值分配给对象的属性,名称为17,如果

对象是一个数组,则该数组的特殊[[Put] ]]方法还观察到

属性名称有资格作为''数组索引''并检查

数组的长度属性是否小于18如果是的话,它会变成18。

为了安全地使用* array *你应该做的事情:


''安全''?你再次喋喋不休。

var arr = new Array();
...
var i = Math.parseInteger(myForm.myFie * ld.value,10);
if(i){arr [i] = quantity;}

这是一个值检查。
要跳过值检查,你可以通过
使用+前缀值(脚本会尝试将
以下表达式转换为数字):

app [+ myForm.myField.value] = quantity;


如果你想确保一个与数组一起使用的属性名称总是

有资格作为''数组索引''那么你会这样做: -


i = String(i>>> 0);


- 因为该操作的内部算法相当于 -

ToString(ToUint32(i)) - 虽然将值转换为字符串

primitive将有点无意义,因为转换隐含在
$ b $中b括号表示法属性访问器。


但是在未事先考虑其性质的情况下,将未知值强制转换为符合数组索引的值

这些价值将被误导为b
。用户输入可能应该在用作数组索引之前使用常规

表达式进行验证,然后不需要强制转换
转换。


< snip>如果你想使用哈希(比如使用项目名称作为你的键),你最好使用通用的Object()构造函数来让你从继承的属性中释放出来。

var hash = new Object();
hash [key] = someValue;

然后:

for(key in hash){
/ / check hash [key]
Or why I just did myArray[''item01''] = "Computers" but
myArray.length is showing 0. What a hey?
You mean like:-

<URL: http://www.jibbering.com/faq/#FAQ4_39 >

- and asking yourself why you expect adding (or assigning a value to) a
named property of an object to have a side effect on another of its
properties.
There is a new trend to treat arrays and hashes as
they were some variations of the same thing. But
they are not at all.
There is no trend, it has always been common for individuals to apply
inappropriate terminology to technical subjects that they don''t fully
understand (even invent their own), and it is common for that action to
cause confusion/misunderstanding in others.
If you are doing *array", then you have to use only integer
values for array index, as it was since ALGOL.
That would depend on how you defined "array index". In ECMAScript ''array
index'' is only a relevant concept in the specification of Array (ECMA
262 3rd edition: section 15.4) and particularly the algorithms for the
Array''s special internal [[Put]] method (ECMA 262 3rd edition: section
15.4.5.1).

An ''array index'' is a _string_ (P), where - ToString(ToUint32(P)) ===
P -. Thus a string representation of a positive 32 bit signed integer (0
through ((2 to the power of 32) - 1)).
Hash (Associative array) doesn''t exists in JavaScript as a
separate programming entity. But each object inherits internal
hash mechanics from Object() constructor.
Javascript is not a class-based language so when inheritance is talked
about in this context it is usually inheritance through the prototype
chain that is being refereed to. Arrays have their own prototype object
but that object has a prototype of its own that is the Object.prototype
object. Thus Arrays inherit all prototyped properties of Object that are
not explicitly defined on the Array.prototype object. But the only
property that Arrays actually inherit from the Object.prototype is the -
valueOf - method, all others are masked by properties defined on
Array.prototype.

All objects, Arrays, Objects, Functions, Regular Expressions, etc, are
instances of the native ECMAScript object. Thus they all have the
characteristics of the native ECMAScritp object, but not as a result of
inheritance, they have the characteristics of that object because they
_are_ that object. One of the primary characteristics of a native
ECMAScript object is that named properties may be added to that object
and assigned values at run-time. This is where the ''hashtable'' and
''associative array''-like features of javascript come from.

The significant difference between an instance of a native ECMAScript
object that is acting as an Array and one that is not is that the Array
object has had its default internal [[Put]] method replaced with a
special alternative that cares whether the property names used to write
to the properties of the object qualify as an ''array index'', and
additionally acts upon the Array''s - length - property, under some
circumstances, if they do.
In hash all keys are CDATA strings
CDATA is not a relevant concept in ECMAScript. String primitives are
sequences of 16 bit Unicode code points.
(even if you provide a number for a key, internally
it''s sorted and treated as a string).
The algorithm for bracket notation property accessors always calls the
internal ToString function on the evaluated result of the expression
within the brackets. This is language related and happens regardless of
whether the object is an Array or not.
Now some JavaScript specifics: as Array extends Object,
it can be also used as a hash.
It can be used as a ''hash'' because it _is_ a native ECMAScript object,
not because it ''extends'' Object.
So you can do something like:

var arr = new Array();
// add to array, arr.length == 1
arr[0] = 10;

// add new property (key/value pair)
// arr.length is *not* affected !
arr[''foo''] = ''JavaScript is funny sometimes'';
Yes you can.
Form values always returned to function as strings.
Gibberish! The ''value'' properties of the DOM representations of form
controls are usually of String type. Functions and return values do not
come into it.
So in a situation like
arr[myForm.myField.value] = 4; // say myForm.myField.value == 17
JavaScript cannot determine what do you want from it: whether
you want to add new property called "17" or you want
to add an array element with index 17.
Javascript knows exactly what to do with that assignment. It assigns a
value to the property of the object with the name "17", and if the
object is an Array the Array''s special [[Put]] method also observes that
the property name qualifies as an ''array index'' and checks to see if the
Array''s length property is less than 18 and makes it 18 if it is.
To work securely with *array* you should do something like:
''Securely''? You are gibbering again.
var arr = new Array();
...
var i = Math.parseInteger(myForm.myFie*ld.value, 10);
if (i) {arr[i] = quantity;}

This is with a value check.
To skip on value check you can do runtime typisation by
prefixing value with "+" (script will try to convert the
following expression into a number):

app[+myForm.myField.value] = quantity;
If you want to make sure that a property name used with an Array always
qualifies as an ''array index'' then you would do:-

i = String( i >>> 0);

- as the internal algorithm for that operation is equivalent to -
ToString(ToUint32(i)) -, though converting the value to a string
primitive would be a bit pointless as that conversions is implicit in
the bracket notation property accessor.

But forcing unknown values into values that qualify as an ''array index''
without prior consideration of the nature of those values would be
misguided. User input should probably be verified with a regular
expression prior to its use as an array index, and then no forcing
conversion would be required.

<snip> If you want to use hash (say using item names as your keys),
you better use the generic Object() constructor to have
you hash free from inherited properties.

var hash = new Object();
hash[key] = someValue;

Then later:

for (key in hash) {
// check hash[key]




当你不需要

数组的开销或副作用时使用一个对象''特殊的[[Put]]方法很有意义。但是如果你想要一个真正的

可枚举的哈希值,那么实现你自己的哈希是最好的选择,因为它可以避免可枚举的原型扩展成为可能的原因

在for-in循环和散列键中可见意外碰撞

规范定义了Object对象的属性。


Richard。



Using an Object when you don''t need the overheads or side effects of the
Array''s special [[Put]] method makes sense. But if you want a real
enumerable hash then implementing your own is the best option as it
avoids the potential for enumerable prototype extensions becoming
visible in for-in loops and hash keys accidentally clashing with
specification defined properties of Object objects.

Richard.


亲爱的理查德,


FAQ 4.39正是我正在反对的:它保证了数组

和哈希(关联数组)是几乎相同的实体,所以你

只需要额外注意一下syntacs。我想要的是什么?
投入的实际情况是,这两个非常不同的实体你需要以非常不同的方式处理


另外请做不要将此视为对JavaScript的攻击。这是一个互联网的基础,所以从一开始它就需要以最简单的方式表达最复杂的东西。它的一些优点在10 - 15年后成为限制并不是它的错。

Dear Richard,

FAQ 4.39 does exactly what I am fighting against: it assures that array
and hash (associative array) are the nearly the same entities, so you
just need to pay some extra attention to the syntacs. What I want to
put in is the truth that these are two very different entities you have
to deal very differently.
Also pls do not look at this as an attack onto JavaScript. It was a
BASIC of Internet, so from the beginning it needed to express the most
complicated things in the most simple way. It is not its fault that
some of its advantages became limitations 10-15 years later.


在文章< 11 *********************@g44g2000cwa.googlegroups。 com>,VK

< sc ********** @ yahoo.com>写道
In article <11*********************@g44g2000cwa.googlegroups. com>, VK
<sc**********@yahoo.com> writes
或者为什么我只是做了myArray [''item01''] =" Computers"但myArray.length显示为0.嘿嘿?

有一种新趋势可以处理数组和哈希,因为它们是同一事物的一些变体。但它们根本不存在。
Or why I just did myArray[''item01''] = "Computers" but myArray.length is
showing 0. What a hey?
There is a new trend to treat arrays and hashes as they were some
variations of the same thing. But they are not at all.



< snip>


这里不是新一轮:-(


为什么单词''hash''。当然你的意思是让你输入一个

的属性名并获得正确的属性值。有几个

实现这种情况的方法。哈希表真的可能吗?


John

-

约翰哈里斯


<snip>

It''s not new round here :-(

Why the word ''hash''. Surely you mean anything that lets you put in a
property name and get out the right property value. There are several
ways of making that happen. Is a hash table really likely ?

John
--
John Harris


这篇关于&LT; FAQENTRY&GT;数组和散列(关联数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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