Tony Collen wrote:

On Fri, 21 Mar 2003, Sylvain Wallez wrote:



Christopher Oliver wrote:



Sylvain Wallez wrote:


<snip/>



Does JavaScript, like Java, make a difference between primitive types
(stored by value on the stack) and object types (stored by reference)
? My understanding after some tests is that a difference is made.


No, there shouldn't be any difference. If there is, it's a bug. What
was your test?



From http://www.forerunners.org/WebLibrary/jscript/ch09_03.htm :


The basic rule in JavaScript is this: primitive types are manipulated by
value, and reference types, as the name suggests, are manipulated by
reference. Numbers and Booleans are primitive types in
JavaScript--primitive because the consist of nothing more than a small
fixed number of bytes, bytes that are very easily manipulated at the low
(primitive) levels of the JavaScript interpreter. On the other hand,
objects and arrays are reference types. These data types can contain
arbitrary numbers of properties or elements, and so can be of arbitrary
size, and cannot be so easily manipulated. Since object and array values
can become quite large, it doesn't make sense to manipulate these types by
value, which could involve the inefficient copying and comparing of large
amounts of memory.

AFAIK, that's just like Java.


Christopher says that Rhino makes no difference between "object" types and primitive types. Now if we consider primitive types as being represented like the immutable objectified Java primitive types (Integer, Boolean, etc), there's not much difference between passing an immutable object by reference and a real primitive type by value.


But that's nitcpicking ;-)

Sylvain

--
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }




Reply via email to