7

What I am looking for is how strings are physically treated in Javascript. Best example I can think of for what I mean is that in the Java api it describes the storage of strings as:

String str = "abc";" is equivalent to: "char data[] = {'a', 'b', 'c'};

To me this says it uses an array object and stores each character as its own object to be used/accessed later (I am usually wrong on these things!)...

How does Javascript do this?

4

2 回答 2

3

字符串是JavaScript 中的String对象。该String对象可以使用[]符号从字符串中获取字符("abc"[0]返回'a')。您也可以使用该String.prototype.charAt功能来实现相同的结果。

侧节点: var a = 'abc'var b = new String('abc')不一样。第一种情况称为原始字符串,String并由 JavaScript 解析器转换为对象。这导致其他数据类型,调用typeof(a)给你stringtypeof(b)给你object

于 2013-03-01T22:56:56.700 回答
0

字符串在 javascript 中以与其他语言存储相同的格式存储。假设 var word = "test" 比 at word 将是一个字符数组,而 't' 将出现在第 0 个位置,依此类推。

采用“word.length”的最后一次迭代将返回未定义。在其他语言中,它返回为 '\0'。

于 2017-08-11T11:32:51.147 回答