 |
|
| View previous topic :: View next topic |
| Author |
Message |
i11uminatus

Joined: 21 Aug 2003 Posts: 110 Location: Toronto, ON
|
Posted: Thu Sep 18, 2003 4:19 pm Post subject: Duplicate object |
|
|
Ok, there's probably some very simple way to do this...but I can't think of it!
Imagine I have an object. Let's call it "hoser" and give it a property.
hoser = new Object();
hoser.favDrink = "beer";
Now, I want to create some instances of other objects: Bob and Doug.
Bob = new Object();
Bob.type = hoser;
//
Doug = new Object();
Doug.type = hoser;
Now Bob and Doug both contain references to the hoser object as their "type".
So if we tried:
trace(Bob.type.favDrink);
We would get "beer" in the output.
Ok, now what if Doug changes his favorite drink to coffee?
If we say:
Doug.type.favDrink = "coffee";
We are essentially changing the property of the "hoser" object.
trace(Bob.type.favDrink);
would now get us "coffee" in the output!
But Bob still likes beer!
So...
Assuming I'm dealing with much more complicated objects and am passing in many properties, how do I then change the property in one object without changing it for all objects?
In other words, how do I make a duplicate of an object (give Bob and Doug each their own unique version of all the properties contained in "hoser") instead of a reference to it?
Thanks
i. |
|
| Back to top |
|
 |
K-dog Moderator

Joined: 23 Jul 2002 Posts: 772 Location: Toronto, ON
|
Posted: Fri Sep 19, 2003 9:21 am Post subject: |
|
|
Excellent question... and I love the Bob and Doug theme!
Let's rework your code a little and see what we get. First let's create a hoser:
| Code: | function Hoser(favDrink, hatColor, name) {
this.favDrink = favDrink;
this.hatColor = hatColor;
this.name = name;
} |
And now let's create Bob and Doug, giving them their own properties:
| Code: | var hoserBob = new Hoser("beer", 0xFF0000, "Bob");
var hoserDoug = new Hoser("beer", 0x00FF00, "Doug"); |
Let's see if they have different names....
| Code: | trace(hoserBob.name); // hey, his name is Bob!
trace(hoserDoug.name); //wow, it's Doug! |
Now let's try to change Doug's favorite drink to coffee:
| Code: | hoserDoug.favDrink = "coffee";
trace(hoserDoug.favDrink); // it's changed!
trace(hoserBob.favDrink); // he still loves beer! |
Right on! Hope that helps, eh?
--K |
|
| Back to top |
|
 |
i11uminatus

Joined: 21 Aug 2003 Posts: 110 Location: Toronto, ON
|
Posted: Mon Sep 22, 2003 9:19 am Post subject: |
|
|
Thanks kristin,
I appreciate your response, and your class solution does give me some perpective, but at the moment I'm not sure it's applicable to this particular scenario.
I will think on it.
AUMMMMMMMMM
i. |
|
| Back to top |
|
 |
pedram
Joined: 26 Nov 2002 Posts: 111 Location: london
|
|
| Back to top |
|
 |
i11uminatus

Joined: 21 Aug 2003 Posts: 110 Location: Toronto, ON
|
Posted: Tue Sep 23, 2003 9:17 am Post subject: |
|
|
pedram,
Thank you for this useful link. I did eventually implement a for-in loop as a solution to my problem, creating a new object and reproducing each property of the target object, as in: | Code: | enemyType = {name:"enemy", speed:4, clipLink:"enemy"};
//
function buildCharacter(name, type){
world[charName] = new Object();
character.type = new Object();
for (var i in type) {
world[charName].type[i] = type[i];
}
} | This is a simplified version, of course. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|