473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Copying objects

Hi!

If A is an object, then "B = A;" will just assign a reference. Isn't there a
simple way to create an actual copy of A and assign that to B?

Greetings,
Thomas

Jul 20 '05 #1
8 4610
"Thomas Mlynarczyk" <bl*************@hotmail.com> writes:
If A is an object, then "B = A;" will just assign a reference. Isn't there a
simple way to create an actual copy of A and assign that to B?


No, not in general.

There are several ways to do something that resembles it, though, and
depending on the objects' nature and how you intend to use the copy,
you can pick the one that suits you best.

If it is a plain object, you can find all the properties you have added
using enumeration, and assign them to another object:

function copyByEnum(object) {
var copyObject = new Object();
for(var property in object) {
copyObject[property] = object[property];
}
return copyObject;
}

This will not work if the object is not a plain object, but, e.g., a
function or array. It will only copy enumerable properties. On an
Object (something created by the user using only "new Object()" and
inheritance), the only non-enumerable properties are the ones in
Object.prototype. The new copy will also have these because it is
created with "new Object()".

If you want to clone an Array, you can first try to detect that it
is an Array, and then use "copy = new Array(object.length)" instead
of "copy = new Object()". Detection isn't that easy, and it won't
help you for functions.

If you don't want to copy all the properties manually, you can use
prototype-based inheritance. In a prototype based language like
Javascript, inheritance happens directly between objects, and not
through classes as in, e.g., Java. The object you inherit from
is called the "prototype", and when you look up properties,
the ones not found in the new object itself are then searched for
in its prototype. Assignments will write to the new object directly.

function copyByInheritance(object) {
function dummyConstructor(){};
dummyConstructor.prototype = object;
return new dummyConstructor();
}

Again, it won't work for host objects like Arrays and Functions.
These copy methods have been shallow copies. If one of the properties
of the object is itself an object, only its reference is copied. If
you want a deep copy, you will need to look at each property and
determine whether it is an object that should be deep-copied again.

All in all, you need a good reason before you start doing any of
this :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
"Thomas Mlynarczyk" <bl*************@hotmail.com> wrote:
If A is an object, then "B = A;" will just assign a reference. Isn't there a simple way to create an actual copy of A and assign that to B?


b = a.clone();
(lowercase letters denote an instance of a class.)

You have to implement the method for each class you write, if you want it to
be cloneable. It's not always simple-- if your object contains references
to other objects, and those contain references to... You have to decide how
deep of a copy to make.

--
Wendy Smoak
Jul 20 '05 #3
"Wendy S" <we******@hotmail.com> writes:
b = a.clone();
(lowercase letters denote an instance of a class.)
I''d say they denote objects. There Are No Classes In Javascript(TM)
(or, if we define something to be called "classes", it will not match,
e.g., Java's notion of a class very well).
You have to implement the method for each class you write, if you want it to
be cloneable. It's not always simple--


Or even possible. In IE (or pretty much anything except Gecko based browsers),
you can't add to the prototype of DOM nodes.

(You should also be careful not to clone the clone functions
unecessarily :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
Also sprach Lasse Reichstein Nielsen:
If it is a plain object, you can find all the properties you have
added using enumeration, and assign them to another object:

function copyByEnum(object) {
var copyObject = new Object();
for(var property in object) {
copyObject[property] = object[property];
}
return copyObject;
}

This will not work if the object is not a plain object, but, e.g., a
function or array. It will only copy enumerable properties.


Thanks for your reply. Meanwhile I have come up with the following:

function copy(s) {
for(p in s)
this[p] = (typeof(s[p]) == 'object')? new copy(s[p]) : s[p];
}

a = new copy(b);

It seems to work as intended. But I haven't yet considered the issue of
arrays or functions. But shouldn't the above work as well with arrays? Are
arrays assigned by reference too, like objects? If so, then I should change
my typeof test to (typeof(s[p]) == 'object' || typeof(s[p]) == 'array').
Would it work for functions too as expected?

Greetings,
Thomas
Jul 20 '05 #5
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:sm**********@hotpop.com...
I''d say they denote objects. There Are No Classes In Javascript(TM)
(or, if we define something to be called "classes", it will not match,
e.g., Java's notion of a class very well).


My apologies! I didn't notice which group the question was posted in and
assumed I was in comp.lang.java.

--
Wendy in Chandler, AZ

Jul 20 '05 #6
"Thomas Mlynarczyk" <bl*************@hotmail.com> writes:
But shouldn't the above work as well with arrays?
It won't. The copy will be an instance of Object, not Array, so it
won't be an array - just an object with property names that look like
numbers.
Are arrays assigned by reference too, like objects?
Arrays are objects. They are host objects, so they can act differently
from user defined objects. You can't inherit "arrayness". You must be
created as an array (either using the constructor "new Array(2,3)" or
a literal "[2,3]").
If so, then I should change my typeof test to (typeof(s[p]) ==
'object' || typeof(s[p]) == 'array'). Would it work for functions
too as expected?


The problem is that "typeof [1,2]" is "object". You have to detect
arrays differently, and there is no absolutely certain way to do
that. You can try testing with
perhapsArray.constructor == Array
perhapsArray instanceOf Array
Array.prototype.isPrototypeOf(perhapsArray)
but they can all be fooled.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
Also sprach Lasse Reichstein Nielsen:
But shouldn't the above work as well with arrays?
It won't. The copy will be an instance of Object, not Array, so it
won't be an array - just an object with property names that look like
numbers.


Ah, I see. So, for practical purposes, my code would copy arrays too, but
not into arrays but objects. The implications would be that I could no
longer use any array specific methods (like join) or properties (like
length) on the result. In addition, my code will convert any properties
which are arrays into objects as well.
perhapsArray.constructor == Array
perhapsArray instanceOf Array
Array.prototype.isPrototypeOf(perhapsArray)
but they can all be fooled.


How could they be fooled? If the array has non-numeric indices? But then it
would be treated as an object anyway, wouldn't it?
Jul 20 '05 #8
"Thomas Mlynarczyk" <bl*************@hotmail.com> writes:
Also sprach Lasse Reichstein Nielsen:

perhapsArray.constructor == Array
perhapsArray instanceOf Array
Array.prototype.isPrototypeOf(perhapsArray)
but they can all be fooled.


How could they be fooled? If the array has non-numeric indices? But then it
would be treated as an object anyway, wouldn't it?


---
function dummyConstructor(){};
dummyConstructor.prototype = new Array();
var nonArray = new dummyConstructor();

alert(nonArray.constructor == Array);
alert(nonArray instanceof Array);
alert(Array.prototype.isPrototypeOf(nonArray));

nonArray[100]=100;
alert(nonArray.length); // not an array after all.
---

You can try testing for array-like behavior:
---
function probablyArray(obj) {
if (! obj.hasOwnProperty("length")) {return false;}
var n = obj.length;
if (obj.hasOwnProperty(n)) {return false;}
obj[n]=true;
if (obj.length != n+1) {return false;}
obj.length = n;
if (obj.hasOwnProperty(n)) {return false;}
return true;
}
---
However, in some browsers, you might be able to make things act like
an array against any test, and still not be an array.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Thomas Lotze | last post by:
Hi, another question: What's the most efficient way of copying data between two file-like objects? f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a string containing...
3
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
6
by: ziwu | last post by:
std::copy() is a function from C++ library. Is memcpy() a function from C library, or is it re-implemented in C++ library? Why people say "In C++, don't use memcpy for non-POD types? What is...
22
by: Matt | last post by:
When browsing a web page a user has the ability to highlight content on a page (by holding down the left mouse button and dragging the mouse over the desired content). Is there a way to disable...
6
by: solex | last post by:
Hello, I am trying to use serialization to copy objects. The object in question "Institution" inherits from a parent object "Party" both are marked as <Serializable()>. Initially I can copy an...
1
by: chris.atlee | last post by:
I'm writing a program in python that creates tar files of a certain maximum size (to fit onto CD/DVD). One of the problems I'm running into is that when using compression, it's pretty much...
5
by: Frederick Gotham | last post by:
I want to copy an array of objects. If it were C, I'd simply do: memcpy( target_array, source_array, sizeof target_array ); However, this won't suffice for objects in C++.
0
by: RS200Phil | last post by:
Hi all, We have this morning used DTS to copy some objects from one database to another. We removed the tick to "Copy all objects" and selected just the 4 tables that we wanted However, we...
9
by: Julian | last post by:
Hi, I have a vector defined like this: std::vector<Load*LoadList; which is populated with different objects of classes that are derived from 'Load'. I need to make a copy of this list during...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.