Copying contents of private data.

170 views
Skip to first unread message

Daniel

unread,
Sep 12, 2001, 1:54:07 PM9/12/01
to
When i set an object to equal another object in script:

a = b; //Where "b" is an object defined by me and contain a private
data pointer.

I notice that the private data pointer in "a" points at the same data
as the pointer in "b". What i really want is to allocate new data and
set the pointer in "a" to point at the new data. Is there some way
control the "copying process" of a self made object?

Brendan Eich

unread,
Sep 12, 2001, 2:19:05 PM9/12/01
to Daniel
Daniel wrote:

> When i set an object to equal another object in script:
>
> a = b; //Where "b" is an object defined by me and contain a private
> data pointer.
>
> I notice that the private data pointer in "a" points at the same data
> as the pointer in "b".

Not only that, but a *is* the identical reference to the object
referenced by b. Object is a reference type. There is no copy of the
object, but not of its private data.

> What i really want is to allocate new data and
> set the pointer in "a" to point at the new data. Is there some way
> control the "copying process" of a self made object?

No, you have to provide your own custom method on such objects, or use
an existing method or "copy constructor" if the class is a native one
defined by the JS object (e.g., Date). Which class of objects are you
thinking of, here?

/be

>


Daniel

unread,
Sep 13, 2001, 8:34:46 AM9/13/01
to
First of all I want to thank you for the speedy reply. Thank you.

Brendan Eich <bre...@meer.net> wrote in message news:<3B9FA719...@meer.net>...


> Daniel wrote:
>
> > When i set an object to equal another object in script:
> >
> > a = b; //Where "b" is an object defined by me and contain a private
> > data pointer.
> >
> > I notice that the private data pointer in "a" points at the same data
> > as the pointer in "b".
>
> Not only that, but a *is* the identical reference to the object
> referenced by b. Object is a reference type. There is no copy of the
> object, but not of its private data.

Now i got a little confused, but I assume that you mean: There IS a
copy of the object, but not of its private data. Otherwise I'm at a
loss.

>
> > What i really want is to allocate new data and
> > set the pointer in "a" to point at the new data. Is there some way
> > control the "copying process" of a self made object?
>
> No, you have to provide your own custom method on such objects, or use
> an existing method or "copy constructor" if the class is a native one
> defined by the JS object (e.g., Date). Which class of objects are you
> thinking of, here?

The class in question is defined by me, and the object is created by
JS_NewObject. To this object I have added a private data pointer. Now,
when the "=" operator is used on the object I want to handle the
copying of the private data differently depending on the class of the
object. What function in the class does the engine call when using the
"=" operator? Or does the engine just copy all the data belonging to
the object (thus copying the pointer and making the "recieving" object
point to the same private data)?

>
> /be
>
> >

/Daniel

Brendan Eich

unread,
Sep 13, 2001, 2:06:17 PM9/13/01
to
Daniel wrote:
First of all I want to thank you for the speedy reply. Thank you.

Brendan Eich <bre...@meer.net> wrote in message news:<3B9FA719...@meer.net>...
Daniel wrote:

When i set an object to equal another object in script:

a = b; //Where "b" is an object defined by me and contain a private
data pointer.

I notice that the private data pointer in "a" points at the same data
as the pointer in "b".
Not only that, but a *is* the identical reference to the object 
referenced by b. Object is a reference type. There is no copy of the
object, but not of its private data.

Now i got a little confused, but I assume that you mean: There IS a
copy of the object, but not of its private data. Otherwise I'm at a
loss.
No, sorry about my confusing use of "no" and "not" in that final sentence I wrote.

There is no copy of any object data.  The only thing that's copied is a *reference* (a safe pointer, if you will) that uniquely addresses the object.

 What i really want is to allocate new data and
set the pointer in "a" to point at the new data. Is there some way
control the "copying process" of a self made object?
No, you have to provide your own custom method on such objects, or use 
an existing method or "copy constructor" if the class is a native one
defined by the JS object (e.g., Date). Which class of objects are you
thinking of, here?

The class in question is defined by me, and the object is created by
JS_NewObject. To this object I have added a private data pointer. Now,
when the "=" operator is used on the object I want to handle the
copying of the private data differently depending on the class of the
object.
Again, there is no copying of any object data, public properties or private structs.

What function in the class does the engine call when using the
"=" operator? Or does the engine just copy all the data belonging to
the object (thus copying the pointer and making the "recieving" object
point to the same private data)?
I hope I've been clear now!  If you believe that script authors will really want to copy objects rather than assign references (as JS's = operator does, and must continue to do), then you need to provide a .clone() or .copy() method.

/be


/be


/Daniel


Daniel

unread,
Sep 14, 2001, 7:04:10 AM9/14/01
to
Brendan Eich <bre...@meer.net> wrote in message news:<3BA0F599...@meer.net>...

Crystal clear. For some reason I was under the impression that objects
were to be considered as instances of classes, rather than references
to instances of classes, and that the "=" operated in a C++ manner.

Thank you for all the help.

/Daniel
>
> /be
>
> >
> >
> >> /be
> >>
> >
> > /Daniel
>
>
>
>
> --

Reply all
Reply to author
Forward
0 new messages