有没有对JS对象键(串)的长度的限制? [英] Is there a limit on length of the key (string) in JS object?

查看:495
本文介绍了有没有对JS对象键(串)的长度的限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,我们不得不在这里我们将有一些JSON,其中关键的是编号(int)和所述值是字符串的情况。但是,我们注意到,大部分的时间,我们仰望基于字符串的ID,所以我们决定改变它,使一个字符串键和值是ID。因为这种方式而不是通过各个项目以及比较数据,我们可能只是做 VAR ID =存储【正文】; 。下面是我们所做的例子。

So we had a case where we would have some JSON, where key is id (int) and the value is the string. But we noticed that most of the times, we look up id based on the string, so we decided to reverse it and make a string the key and the value is the id. Because that way instead of going through each item and compare values, we could just do var id = storage[text];. Below are the examples of what we did.

下面是老实施例如:

var storage = {
  0 : null,
  1 : "Hello",
  2 : "world!",
  3 : "How are you?"
}

下面是新实施的例子:

var storage = {
  "null" : 0,
  "Hello" : 1,
  "world!" : 2,
  "How are you?" : 3
}

据我所知,现在的字符串是关键,它的确定以获得相同的ID为相同的字符串。但由于现在的字符串可以是潜在的pretty的巨大的(机会渺茫,但每串可能是最大1KB),是有长度的限制JS或Android的WebView穿的对象键?

和也,这是否实施有缺点?我还没有发现任何问题,到目前为止,但你永远不知道。

And also, does this implementation have disadvantages? I haven't noticed any issues so far, but you never know.

推荐答案

没有,没有限制的字符串长度(只要它适合到内存),以及实现似乎也没有问题。这实际上可以很常见的那些'转身'阵列,例如布尔值。 而作为以字符串作为键:这些字符串存储在一个特定的地址不变的符号,以及实际上作为指数为阵列的地址(也就是指针又名参考),而不是字符串本身

No, there is no limit for the string length (as long as it fits into memory), and your implementation seems okay too. It's acutally quite common to have those 'turned around' arrays with e.g. boolean values. And as to the strings as keys: The strings are immutable symbols that are stored at a certain address, and what's actually used as the index for the array is that address (aka pointer aka reference) and not the string itself.

这篇关于有没有对JS对象键(串)的长度的限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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