Can a String hold Array Object? May be this question could be silly? Just wanted to k开发者_Python百科now...
No. You cannot extend strings beyond adding methods to String.prototype
.
For example:
> var x = 'foo'
undefined
> x
"foo"
> x.bar = 4
4
> x
"foo"
> x.bar
undefined
If you serialize (ie turn the object into a string) then yes a string can hold an array object. http://en.wikipedia.org/wiki/Serialization
A string can just "hold" characters - that's what a String is.
Of course, those characters could denote a stringified representation of an array of objects, such as the string [1, "two", MyCoolClass(5, 8.0)]
. But that would just be some quasi-arbitrary encoding, and you'd have to do some work to convert it back again (see JSON for a real-world example).
So the short answer is "no", but the real answer would be to ask for clarification of what it is you mean...
In theory yes, you could probably encode a set of objects into a string. Using JSON and a little bit of string acrobatics.
It would wind up looking like this
"{ field: var ... },...,{ field: var ... }"
or something like that.
You can use array referencing on strings, to access a specific letter:
a = "hello world"
a[0] // == 'h'
a[1] // == 'e'
use JSON.stringify(arrayToStringiFy)
link to download the library JSON
精彩评论