Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #7956

Newbie Q's: references, scopes, overwrites

From Dom Bannon <nospam@nospam.com>
Newsgroups comp.lang.javascript
Subject Newbie Q's: references, scopes, overwrites
Date 2011-11-03 18:07 +0000
Message-ID <fbk5b7dp4brgoenop5rogsol1jv2lveatj@4ax.com> (permalink)
Organization Zen Internet

Show all headers | View raw


Sorry for the newbie question - I write several languages but my
JavaScript is very limited. I have a problem with some code which
displays an svg file received via Ajax. The top-level script includes
these lines:

1  var svg = xhr.responseXML.documentElement;
2  svg = cloneToDoc(svg);  // portable importNode
3  window.svgRoot = svg;
4  document.body.appendChild(svg);
5  delete window.svgRoot;  // <- problem

The script then terminates. The incoming Ajax message ('svg') is a
script, that references a couple of additional scripts, and I rely on
one of them to be run when it's loaded (presumably on 'appendChild',
but I'm not sure).

This works, but only if line 5 is commented out. If it's left in, I
get various issues which have the feel of a memory over-write. I don't
really know what to do here, because I don't understand the window
object, or the scoping issues, or much else. The guy who wrote it says
that line 5 is "just cleanup, removing the global svgRoot property
from the window after appending the file".

Dumb question #1: what exactly is 'window'? Is it an object which
represents the currently visible window? Presumably it's still in
scope, and global, and represents the same window when the other (svg)
scripts are running?

#2: what is line 3 doing? Is it auto-declaring a new 'svgRoot' var in
'window'? Can you just do that? Is the author just finding a
convenient location to put a global?

#3: the rest of the code manipulates the svg via window.svgRoot,
rather than getting a reference to the new child in the document body.
Is this Ok? Is line 4 appending a reference to svg, or copying svg, or
somehting else? Presumably line 5 doesn't actually delete the new
child?

#5: is there a memory overwite/leak detect tool for JavaScript?

Thanks if you managed to read this far... :) 

Back to comp.lang.javascript | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Newbie Q's: references, scopes, overwrites Dom Bannon <nospam@nospam.com> - 2011-11-03 18:07 +0000
  Re: Newbie Q's: references, scopes, overwrites Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-03 20:11 +0100
    Re: Newbie Q's: references, scopes, overwrites Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-03 20:17 +0100
    Re: Newbie Q's: references, scopes, overwrites Dom Bannon <nospam@nospam.com> - 2011-11-03 20:07 +0000
      Re: Newbie Q's: references, scopes, overwrites Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-03 22:33 +0100
        Re: Newbie Q's: references, scopes, overwrites Antony Scriven <adscriven@gmail.com> - 2011-11-03 19:32 -0700
  Re: Newbie Q's: references, scopes, overwrites Elegie <elegie@anonymous.invalid> - 2011-11-03 20:54 +0100

csiph-web