node object gets clobbered after clone

R

Richard Trahan

I'm a js newbie, and I'm sure I'm missing something; please help me
find it.

Consider these fragments:

function reconstruct_select_node(selectnode,nodearray) {
alert("120: "+typeof(selectnode));
newselnode = selectnode.cloneNode(false); // shallow clone
... (operations only on newselnode)
alert("139: "+typeof(selectnode));
return newselnode;
}
....
ar = new Object();
ar.selectgraphsnode = (some node object)
....
alert("405: " + typeof(ar.selectgraphsnode));
newselnode = reconstruct_select_node(ar.selectgraphsnode,sna);
alert("407: " + typeof(ar.selectgraphsnode));

The alerts at lines 120, 139, and 405 report type "Object", but
the alert at line 407 is "undefined".

I have an easy workaround for this, but I'd like to understand
what is happening.

Any help, please. Thanks.
 
L

Lasse Reichstein Nielsen

Richard Trahan said:
function reconstruct_select_node(selectnode,nodearray) {
alert("120: "+typeof(selectnode));
newselnode = selectnode.cloneNode(false); // shallow clone

Notice that "newselnode" is not declared as a local variable, so it
is created as a global one. That is probably not your intent (no
need to return it in that case), so you might vant to add "var" in
front of "newselnode" here.
... (operations only on newselnode)

They don't affect "ar"?
alert("139: "+typeof(selectnode));
return newselnode;
}
...
ar = new Object();
ar.selectgraphsnode = (some node object)
...
alert("405: " + typeof(ar.selectgraphsnode));
newselnode = reconstruct_select_node(ar.selectgraphsnode,sna);

What is "sna"'s value?
alert("407: " + typeof(ar.selectgraphsnode));

The alerts at lines 120, 139, and 405 report type "Object", but
the alert at line 407 is "undefined".

Try alerting the type of "ar.selectgraphsnode" at line 102 and 139
as well.
I have an easy workaround for this, but I'd like to understand
what is happening.

So would I, but you have to supply more code. Obviously, something
goes wrong somewhere, and I'll bet it's in the code we cannot see.
If you have a page that exhibits the problem, give us a link.

Good luck.
/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top