On Fri, Jul 13, 2012 at 1:09 PM, Charles Han
<[email protected]>wrote:

> Thanks for you reply. I can see what you mean in JavaScript but I don't
> understand how the data structure in the V8 Array, especially with all the
> JSON objects in the array.
>
> Can you please explain a bit more?
>


An Array object holds arbitrary Value values, and each Value refers to one
of the following value types:

- Object
- Array
- Number
- Null
- String
- Undefined
- Function
- Regex

What your code does is extracts a value from the array (the value just
happens to be-a Object) and then calls toString() on it. The result of
calling toString() on an Object is "[Object object]", which is why you see
that output. There is no generic, 1-size-fits-all object-to-string
conversion. If you want to output the contents of an Object, you must first
fetch those contents from the object or use a generic object-to-string
mechanism like JSON.stringify().

On a related note: PLEASE don't use printf() for this type of thing. The v8
example code uses it and is a sign that an absolutely C++ beginner wrote
that code (or that whoever wrote it doesn't care much for code quality). In
C++ one uses std::cout and std::cerr for this type of simple output, not
printf(), and _especially_ when only outputting a string as-is with no
formatting. The peformance difference between:

printf("%s\n", "foo")

and:

puts("foo")

is absolutely staggering (the second one is MUCH more efficient), but the
end result is identical. printf() has its place, but (printf("%s\n",...))
is ALWAYS a poor choice - puts(...) is faster, uses less stack space, and
is not subject to formatting-related injection bugs.

-- 
----- stephan beal
http://wanderinghorse.net/home/stephan/
http://gplus.to/sgbeal

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to