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


Groups > comp.lang.javascript > #7958

Re: Newbie Q's: references, scopes, overwrites

Message-ID <3004336.SPkdTlGXAF@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-11-03 20:11 +0100
Subject Re: Newbie Q's: references, scopes, overwrites
Newsgroups comp.lang.javascript
References <fbk5b7dp4brgoenop5rogsol1jv2lveatj@4ax.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Dom Bannon wrote:

> 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.

That is to be expected.  But does it terminate with an error?  If yes, what 
does the error message say?

> 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 is not going to happen.

> This works,

It is generally unreliable.  The only way to be rather sure that new script 
code will be executed is to append a new `script' element.

This might be an exception if the parent document is declared XHTML 1.1 + 
SVG + MathML, or HTML5, and you are actually appending an `svg' element 
node, where the script that it contains pertains to and is triggered by SVG 
elements only.  But your description indicates otherwise.

> but only if line 5 is commented out.

Line 5 attempts to delete a property of a host object, which may not be 
possible.

> If it's left in, I get various issues which have the feel of a memory
> over-write.

What issues?  How did you get that impression?

> I don't really know what to do here, because I don't understand the window
> object, or the scoping issues, or much else.

This does not appear to have anything to do with scope.

> The guy who wrote it says that line 5 is "just cleanup, removing the
> global svgRoot property from the window after appending the file".

That description is correct in general, but they are otherwise clueless.  
See below.

> Dumb question #1: what exactly is 'window'?

It is a host-defined property of the ECMAScript Global Object which value is 
a reference to an object that represents the view of the document that 
contains the script code.

> Is it an object which represents the currently visible window?

In a sense, yes.

> Presumably it's still in scope, and global, and represents the same window
> when the other (svg) scripts are running?

This is not a valid question.  Please restate your request.

> #2: what is line 3 doing?

It is an attempt to create or overwrite the property of the object referred 
to by `window', that is named `svgRoot', with the value of the `svg' 
variable.

> Is it auto-declaring a new 'svgRoot' var in 'window'?

No.

> Can you just do that?

No.

> Is the author just finding a convenient location to put a global?

Yes, and he might have been misguided by Douglas Crockford's notion that 
this was the (proper) way to do that.
 
> #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?

That depends on whether the object referred to by `window' already had a 
`svgRoot' property in the first place, whether it has one after the 
assignment in line 3, and whether the value of that property is the value of 
`svg'.

The property creation may fail because the object referred to by `window' is 
a host object (not a native one), and the property value modification may 
fail because an existing property of that name was defined to be read-only.

> Is line 4 appending a reference to svg, or copying svg, or
> somehting else?

Yes.

> Presumably line 5 doesn't actually delete the new child?

Please restate your request.

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

Yes.
 

PointedEars
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
 -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next 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