How are strings physically stored in Javascript

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?


Strings are String objects in JavaScript. The String object can use the [] notation to get character from a string ( "abc"[0] returns 'a' ). You can also use the String.prototype.charAt function to achieve the same result.

Side node: var a = 'abc' and var b = new String('abc') are not the same. The first case is called a primitive string and get converted to a String object by the JavaScript parser. This results in other data types, calling typeof(a) gives you string but typeof(b) gives you object .


Strings are stored in the same format in javascript as other languages stores. Suppose var word = "test" than at word will be as an array of characters and the 't' will come at 0th position and so on.

The last iteration as taking 'word.length' will return undefined. In other languages, it returns as ''.

链接地址: http://www.djcxy.com/p/12138.html

上一篇: 如何用JSDoc3记录AMD + Backbone项目

下一篇: 字符串是如何物理存储在Javascript中的