Jump to content

simple custom object question


Kreplock

Recommended Posts

i have two instances of a custom object, and would like to copy the values of one into the other:

obj2 = obj1

i quickly noticed obj2 gets populated with the address of obj1, not the values. so when i reinitialise obj1 i lose my precious values in obj2 as well.how do i copy the values instead of the location?

Link to comment
Share on other sites

what do u mean by adress, sounds like a pointer 2 me
yep. when you instantiate an object it gets a chunk of memory on the heap, with a pointer holding the address where that memory starts. something like that, anyway.so i have two different objects (obj1 and obj2) on the heap until i execute: obj2 = obj1instead of reading the values of obj1 and populating obj2 with copies of those values, it looks like it just overwrites obj2's pointer, so that i now have only 1 object, obj1 and obj2 both pointing to it. the second object no longer has a name pointing to it, so it's gone forever.
Link to comment
Share on other sites

Instead of making them equal to each other like your doing, why not create a prototype type function for that object that copies the values of one object, to the other?function Object() {blah}Object.prototype.copy = function(obj) {blah obj blah}var 1 = new Object();var 2 = new Object();1.copy(2);

Link to comment
Share on other sites

if i understand your suggestion right it would resemble this:

function dog() {	this.name = name;// ...list of properties}dog.prototype.copy = function(obj) {	obj.name = this.name;// ...list of assignment statements for each property}var dog1 = new dog();var dog2 = new dog();dog1.name = "fluffy";dog2.name = "rex";document.write(dog1.name); // prints "fluffy"document.write(dog2.name); // prints "rex"dog1.copy(dog2);document.write(dog1.name); // prints "fluffy"document.write(dog2.name); // prints "fluffy"

that works, but it requires what i was trying to avoid in the first place: manually typing out an assignment statement for every property in an object copy method, which needs to be updated every time the object definition gets changed.i cannot believe JavaScript lacks an easy, one-line solution for copying the values of one object into another object of the same type. it's just not possible that someone would create a language that obtuse. but i cannot find an example of the syntax.i noticed arrays behave the same way.array1 = array2;kills array1 and both names now point to the same array. that's fine, but there must be a way to copy by value, keeping each array separate. i tried:array1[] = array2[];array1() = array2();(array1) = (array2);but none are valid.curiously, document.write(array2); behaves the way i'd expect and returns the entire list of elements, though with those pesky commas.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...