Link to home
Start Free TrialLog in
Avatar of mb2297
mb2297

asked on

Discovering Javascript memory addresses

Hello experts,

Is there any way to find out the memory address where a variable is stored by Javascript?

Thanks,
Matt.
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mb2297
mb2297

ASKER

Hi heilo,

Clearly the concept of "pointers" exists, just called by another name. If there's no way of identifying the memory address where a variable lives, is there any way to distinguish two 'references' to the same object?
>>is there any way to distinguish two 'references' to the same object?
No. You can only determine the object type by means of "instanceof":
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Operators:Special_Operators:instanceof_Operator

OR by using the "constructor" property, but again, these are just ways to determine the object type (String, Date, etc), rather than "tell me to which object instance this variable refers to".

May I ask the context for this? Perhaps creating a custom object that allows a single instance of itself is the way to go.
Avatar of mb2297

ASKER

It's really for debugging purposes. I'm using an existing codebase, and a copy operation is not working as expected. See the attached code snippet for an example.

You'd expect that ob1 and obj3 would end up looking the same (excepting the 'name' properties). If you run that code, they are, but in my project, I have to explicitly copy the properties across. The added complication with my project is that the properties being copied are also references, rather than primitives.

I've had problems like this in Java, and debugged them by comparing memory addresses to understand what's actually going on underneath. Also, I'm just curious about Javascript's memory management - I'd like to have a bit more transparency.
	var obj1 = {
		name : 'obj1',
		subobj : {
			prop1 : 'foo',
			prop2 : 'bar',
			prop3 : 'baz'
		}	
	}
	
	var obj2 = {
		name : 'obj2',
		subobj : {
			prop1 : 'oof',
			prop2 : 'rab',
			prop3 : 'zab'
		}	
	}
	
	var obj3 = {
		name : 'obj3',
		subobj : {
			prop1 : 'foo',
			prop2 : 'bar',
			prop3 : 'baz'
		}	
	}
	
	var obj4 = {
		name : 'obj4',
		subobj : {
			prop1 : 'oof',
			prop2 : 'rab',
			prop3 : 'zab'
		}	
	}
 
	obj1.subobj.prop1 = obj2.subobj.prop1
	obj1.subobj.prop2 = obj2.subobj.prop2
	obj1.subobj.prop3 = obj2.subobj.prop3
	console.log(obj1)
	
	obj3.subobj = obj4.subobj
	console.log(obj3)

Open in new window

>> I have to explicitly copy the properties across
You don't HAVE to! Look at the code below

>>I'm just curious about Javascript's memory management
Once a variable has been set to some value, if you then want to free memory, just set it to null. Javascript will do automatic garbage collection. For array elements or objects, you can call the delete function explicitly:
var x = new Object();
delete(x);
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript"><!--
function cloneObject(o){
	var t = new Object();
	for( i in o )
	{
		if("object"==typeof(o[i]) )
		{
			t[i] = cloneObject(o[i]);
		}
		else
		{
			t[i] = o[i];
		}
	}
return t;
}
 
var obj1 = {
		name : 'obj1',
		subobj : {
			prop1 : 'foo',
			prop2 : 'bar',
			prop3 : 'baz'
		}	
	};
var obj3 = cloneObject(obj1);
obj3.name="obj3";
alert(obj1.name + " " + obj3.name);
//--></script>
</body>
</html>

Open in new window

Avatar of mb2297

ASKER

Thanks hielo,

I'd already considered a clone() function, but it's ultimately still 'explicitly copying' the properties - it just wraps them in a subroutine. What I was really after was a way to just change the reference to subobj, and therefore avoid a whole bunch of memory copies.
>>What I was really after was a way to just change the reference to subobj
See the code below. It think you are interested in the functionality portrayed by "t" below.
<script type="text/javascript"><!--
 
	var obj3 = {
		name : 'obj3',
		subobj : {
			prop1 : 'x',
			prop2 : 'bar',
			prop3 : 'baz'
		}	
	}
	
	var obj4 = {
		name : 'obj4',
		subobj : {
			prop1 : 'y',
			prop2 : 'rab',
			prop3 : 'zab'
		}	
	}
 
	alert("Originally:\n\n" + obj3.subobj.prop1 + " " + obj4.subobj.prop1)
 
	//now make obj3's subojb "point" to obj4's subobj
	obj3.subobj = obj4.subobj;
 
	//t gets a reference to obj3 subobj
	var t = obj3.subobj
 
	alert("Post obj.property-to-obj.property assignment:\n\n"+obj3.subobj.prop1 + " " + obj4.subobj.prop1 + " " + t.prop1)
 
	//now change one of obj4 subproperties' values
	obj4.subobj.prop1 = "z";
 
	//now let's see if it "propagates" to obj3
	alert("After changing original node:\n\n"+obj3.subobj.prop1 + " " + obj4.subobj.prop1 + " " + t.prop1)
	t.prop1 = "TBag"
 
	alert("After changing via t:\n\n"+obj3.subobj.prop1 + " " + obj4.subobj.prop1 + " " + t.prop1)
 
//--></script>

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mb2297

ASKER

Thanks all.

@hielo - that's a nice demonstration of manipulating references, but it doesn't help me. I'm trying to work out why that logic can't be applied in my situation.

@ahoffman - strict comparison is helpful, thank you. I think I can forgive Javascript this - I can't see myself going back to C!



> .. going back to C!
for sure, you need to go forward for that :-]]
good luck.