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.
mb2297Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ahoffmannConnect With a Mentor Commented:
> You only pass references,
not realy true, simple data types are passed by value

>>> .. is there any way to distinguish two 'references'
>> No. You can only determine the object type by means of "instanceof":
there is the strictly equal === operator (JavaScript 1.3, see http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Operators:Comparison_Operators)
@mb2297, this is probably what you're looking for

> .. Javascript will do automatic garbage collection.
sometimes, if you're lucky :-/

> For array elements ... you can call the delete function explicitly:
yes you can call delete() for arrays, but it does not nullify them, you have to use:
  arrayvar.length=0;
(and in some older JavaScript implementations you even have to remove all array elements with pop() to clear memory):-

> .. just change the reference to subobj, and therefore avoid a whole bunch of memory copies
good idea, but the you have to use another language, how about C?
:-))
0
 
hieloConnect With a Mentor Commented:
Absolutely not! There are no "pointers" in javascript. You only pass references, but you neve know the addresses of those references.
0
 
mb2297Author Commented:
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?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
hieloCommented:
>>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.
0
 
mb2297Author Commented:
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

0
 
hieloCommented:
>> 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

0
 
mb2297Author Commented:
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.
0
 
hieloCommented:
>>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

0
 
mb2297Author Commented:
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!



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

From novice to tech pro — start learning today.