Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Catching spidermonkey copying a native object

2 views
Skip to first unread message

Warrick

unread,
Apr 5, 2004, 4:43:01 PM4/5/04
to
Hi,

I'm a bit confused as to how the copying of objects work. I have
embedded a native object and that all works fine so I can do:

var v = new NativeObj();

but then if I do:

var v2 = v;

I'm not getting my native constructor called that I defined in my
jsclass structure? I want to catch the copy as I need to do 'special
native' things if the object is copied.

I'm sure I'm missing something simple!

Brendan Eich

unread,
Apr 5, 2004, 5:24:17 PM4/5/04
to Warrick

You seem to be assuming that 'var v2' is akin to saying, in C++,
'NativeObj v2', and that the C++ counterpart-class NativeObj has a
copy-constructor.

JS is not C++. There is on deep or shallow copy on assignment. Object
is a reference type, so v2 = v just copies the reference in v into v2,
making both variables denote the same object.

If you want to copy an object, you'll need to define a copy or clone
method on it that does the right thing. "The right thing" may mean
something quite different depending on use-cases, so beware of bogus
generality.


Probably if you're embedding JS without knowing its rules, you should
get a book, and perhaps dive into the ECMA spec. The Flanagan O'Reilly
book, and Danny Goodman's JavaScript bible, or good books. The ECMA-262
Edition 3 spec is at http://www.mozilla.org/js/language/E262-3.pdf.

/be

0 new messages