数组作为哈希表 [英] Array as hash tables

查看:77
本文介绍了数组作为哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


众所周知,数组可以用作哈希表:


var color = [];


color [" red"] = 0xFF0000;

color [" blue"] = 0x0000FF;

有了这个,我可以检查字符串是否标识颜色:


函数isColor(字符串)

{

返回颜色[string] == undefined;

}


我唯一关心的是


isColor(" pop")= = true


,这将适用于相当多的烦人,寄生,

"关键字" (构造函数,长度等...)


我终于使用以下作为裸体对象,用作

哈希表:


函数HashTable(){this.constructor = undefined; }


//使用示例:

var color = new HashTable;

color [" red"] = 0xFF0000

专家的问题:不会有其他意外的,硬编码的,

关键字在黑暗中跟踪我的字符串,并返回晦涩难懂,

native,js对象,我在哪里预期普通数字?


任何帮助表示赞赏,

Alexis

Hello,

It is common knowledge that arrays can be used as hashtables:

var color = [];

color["red"] = 0xFF0000;
color["blue"] = 0x0000FF;
With this, I can check whether a string identifies a color, or not:

function isColor(string)
{
return color[string] == undefined;
}

My only concern is that

isColor("pop") == true

and that will hold true for quite a few annoying, parasitic,
"keywords" ("constructor", "length", etc...)

I finally came to use the following as a naked object, to use as a
hashtable:

function HashTable() { this.constructor = undefined; }

// example of use:
var color = new HashTable;
color["red"] = 0xFF0000
Question for experts: won''t then there be other unexpected, hardcoded,
keywords stalking my strings in the dark, and returning obscure,
native, js objects, where I expected plain numbers ?

Any help appreciated,
Alexis

推荐答案

Alexis Nikichine写道:
Alexis Nikichine wrote:
众所周知,数组可以用作哈希表:

var color = [] ;

color [" red"] = 0xFF0000;
color [" blue"] = 0x0000FF;

有了这个,我可以查看一个字符串是否识别颜色:

函数isColor(字符串)
{
返回颜色[string] == undefined;
}
我唯一担心的是

isColor(" pop")== true

并且这将适用于相当多的恼人的,寄生的,关键词。 (构造函数,长度等等...)

我终于使用以下作为裸体对象,用作
散列表:

函数HashTable(){this.constructor = undefined; }

//使用示例:
var color = new HashTable;
color [" red"] = 0xFF0000

专家问题:难道不会有其他意外的,硬编码的,关键词在黑暗中跟踪我的字符串,并返回晦涩的,原生的,js对象,在那里我期待普通数字吗?
任何帮助表示感谢,
It is common knowledge that arrays can be used as hashtables:

var color = [];

color["red"] = 0xFF0000;
color["blue"] = 0x0000FF;
With this, I can check whether a string identifies a color, or not:

function isColor(string)
{
return color[string] == undefined;
}

My only concern is that

isColor("pop") == true

and that will hold true for quite a few annoying, parasitic,
"keywords" ("constructor", "length", etc...)

I finally came to use the following as a naked object, to use as a
hashtable:

function HashTable() { this.constructor = undefined; }

// example of use:
var color = new HashTable;
color["red"] = 0xFF0000
Question for experts: won''t then there be other unexpected, hardcoded,
keywords stalking my strings in the dark, and returning obscure,
native, js objects, where I expected plain numbers ?

Any help appreciated,




如果索引不是整数,则使用Object,而不是数组。


var color = {};


如果下标是标识符(而不是保留字),你可以使用时髦的点符号




color.red = 0xFF0000;

color.blue = 0x0000FF;


在HTML应用程序中,它可能更好保持颜色值为字符串。

这样你就不会在黑色上得到假阴性。


color.red ="#FF0000" ;;

color.blue ="#0000FF";

color.black ="#000000" ;;


如果您使用的是小型学科,那么可以将您的

值与Object方法区分开来。


function isColor(string){

返回typeof color [string] ==" string";

}


上面的哈希表构造函数是愚蠢的。如果它没有方法,那么最好使用普通的Object。更好的是,你可以使用

对象文字表示法。


color = {red:"#FF0000",blue:"#0000FF",黑色:#000000};

http://www.JSON .org


Douglas Crockford写道:
Douglas Crockford wrote:
如果索引不是整数,那么使用一个Object,而不是一个数组。

var color = {};

如果下标是一个标识符(而不是保留字),你可以使用时髦的点符号


color.red = 0xFF0000;
color.blue = 0x0000FF;

在HTML应用程序中,保留颜色
可能更好[snip> ;


颜色示例只是一个无偿的例子。我担心

用户输入的任意字符串。有一天,我有报道流行艺术

请求失败。 "弹出"产生了一个函数(而不是预期的

整数)。

如果你使用一个小类型的规则,那么你可以将
的值与Object方法区分开来。

函数isColor(string){
返回typeof color [string] ==" string" ;;
}


我第一次遇到了,我使用了类型规则,并确保

返回的值是一个整数。

上面的哈希表构造函数是愚蠢的。如果它没有方法,那么最好使用普通的对象。更好的是,你可以使用
对象文字表示法。
If the indexes are not integers, then use an Object, not an Array.

var color = {};

If the subscript is an identifier (and not a reserved word), you can use the stylish dot notation.

color.red = 0xFF0000;
color.blue = 0x0000FF;

In HTML applications, it may be better to keep color [snip>

The color example was just a gratuitous one. I was worrying about
arbitrary strings typed in by users. One day, I had reports of "pop art"
request failing. "pop" yielded a function (instead of an expected
integer).
If you use a little type discipline, then it is eay to distinguish your values from Object methods.

function isColor(string) {
return typeof color[string] == "string";
}
When I first met the, I used type discipline, and made sure that the
returned value was an integer.
The hashtable constructor above is silly. If it doesn''t have methods,
then it is better to use a plain Object. Even better, you can use the
object literal notation.




不幸的是,我现在不能依赖类型训练,因为(在另一个

尿素化的情况)我需要一个哈希(并且不要叫我perl boob :-)

返回哈希,整数。


使用普通对象,我会在构造函数上得到意想不到的结果。


这就是我要问的原因:我的HashTable真的很空白

对象/函数,一旦是构造函数,会员已被空白?


Alexis


-

某个域名是免费的(顺便说一句,我所有的一切知道了,我从你的

网站上学到了什么;谢谢:-)


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!



Unfortunately, I can''t rely on type discipline, now, since (in another
urelated case) I need an hash (and don''t call me a perl boob :-)
returning hash, integers.

With a plain Object, I will get an unexpected result on "constructor".

Which is why I am asking: is my HashTable a really really empty
Object/Function, once the "constructor" member has been blanked ?

Alexis

--
some domain is free (and btw, everything I know, I learned from your
site; thank you :-)

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


>>如果索引不是整数,则使用Object,而不是数组。
>>If the indexes are not integers, then use an Object, not an Array.

var color = {};

如果下标是标识符(而不是保留字),你可以

var color = {};

If the subscript is an identifier (and not a reserved word), you can



使用



use

时尚的点符号。

color.red = 0xFF0000;
color.blue = 0x0000FF;
the stylish dot notation.

color.red = 0xFF0000;
color.blue = 0x0000FF;

In HTML applications, it may be better to keep color



[snip>

颜色示例只是一个无偿的例子。我担心用户输入的任意字符串。有一天,我有报道称波普艺术请求失败。 "弹出"产生了一个函数(而不是预期的整数)。



[snip>

The color example was just a gratuitous one. I was worrying about
arbitrary strings typed in by users. One day, I had reports of "pop art"
request failing. "pop" yielded a function (instead of an expected
integer).

如果你使用一个小类型的纪律,那么就可以区分
If you use a little type discipline, then it is eay to distinguish

<来自Object方法的



your

值。

函数isColor(string){
返回typeof color [string] = =" string";
}
values from Object methods.

function isColor(string) {
return typeof color[string] == "string";
}



当我第一次见到时,我使用了类型规则,并确保
返回的值是一个整数。 br />


When I first met the, I used type discipline, and made sure that the
returned value was an integer.

上面的哈希表构造函数很傻。如果它没有方法,那么最好使用普通的对象。更好的是,你可以使用
对象文字表示法。
The hashtable constructor above is silly. If it doesn''t have methods,
then it is better to use a plain Object. Even better, you can use the
object literal notation.



不幸的是,我现在不能依赖类型训练,因为(在另一个 case)我需要一个哈希(并且不要叫我perl boob :-)
返回哈希,整数。

使用普通的Object,我会得到一个意想不到的结果;构造函数>

这就是我要问的原因:我的HashTable是一个非常空的对象/函数,一旦是构造函数会员已被空白?


Unfortunately, I can''t rely on type discipline, now, since (in another
urelated case) I need an hash (and don''t call me a perl boob :-)
returning hash, integers.

With a plain Object, I will get an unexpected result on "constructor".

Which is why I am asking: is my HashTable a really really empty
Object/Function, once the "constructor" member has been blanked ?




是和否。新对象为空,但它从其

原型链继承值。 Clobbering构造函数不会改变它。


如果您的对象(或胸部,哈希)中的值不是

函数,您可以使用


(typeof color [string]!=''function'')


来区分你的值和Object方法。


顺便说一下,我认为这是JavaScript中的设计错误。这些方法的外观复杂化了对象作为集合的使用。

http://www.crockford.com/javascript/survey.html


这篇关于数组作为哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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