Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object copying 1

Status
Not open for further replies.

jtaal

Technical User
Jan 17, 2004
23
NL
Hi all,

Is there a way to make the following script work as i would expect it to?
Code:
<script language="javascript">
function constr () {
  this.value = 'oldval';
  this.someotherval = 'xxx';
}

var obj = new constr ();
obj.someotherval ='yyy';
var obj2 = obj;
obj2.value = 'newval';
alert(obj.value);
</script>

I would expect an alert ('oldval'); but instead newval is alerted.
I think javascript uses (alot) of Cpointer-like (or C++reference-like) copying.
Is there a way to let javascript do some real copying???
 
according to my js book, objects are reference values and so copying is done by reference. the solution is to make your own copy method:
Code:
<script language="javascript">

function constr () {
  this.value = 'oldval';
  this.someotherval = 'xxx';
}

function realCopy(o) {
	var tmp = new Object();
	
	for (p in o)
		tmp[p] = o[p];
	
	return tmp;
}


var obj = new constr ();
obj.someotherval ='yyy';

var obj2 = realCopy(obj);
obj2.value = 'newval';

alert(obj.value);

</script>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
I already thought of this. The only problem is that I hav recursive objects and i want them to be copyed (not reffed) as well. I don't think this will work for my application.
 
Code:
<script language="javascript">

function constr () {
  this.value = Object ();
  this.value.val = 'oldval';
  this.someotherval = 'xxx';
}

function realCopy(o) {
    var tmp = new Object();
    
    for (p in o)
        tmp[p] = o[p];
    
    return tmp;
}


var obj = new constr ();
obj.someotherval ='yyy';

var obj2 = realCopy(obj);
obj2.value.val = 'newval';

alert(obj.value.val);
</script>
 

I can't really add much to that but I was gonna suggest a slightly different function.

Code:
function objCopy( sourceObj ) {

	for( var prop in sourceObj ) {
		this[ prop ] = sourceObj[ prop ];
	}
}

var obj = new constr ();
var obj2 = new objCopy( obj );

Same difference, eh.
 
Could i try something with a recursive (?) function that copies with a special copy if the type is an Object and normal copy else?
Does an Array has the same behaviour???
 


Seems to work okay.

Code:
function objCopy( sourceObj ) {

	for( var prop in sourceObj ) {

		if ( typeof sourceObj[ prop ] == "object" )
			this[ prop ] = new objCopy( sourceObj[ prop ] );
		else
			this[ prop ] = sourceObj[ prop ];
	}
}

HTH.
 
This is awesome, working around javascripts reference copying. Do you know if the same behaviour goes for the Array () object?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top