Re: JavaScript knowledge test
- From: "Richard Cornford" <Richard@xxxxxxxxxxxxxxxxxxx>
- Date: Thu, 2 Aug 2007 00:04:07 +0100
On Aug 1, 6:42 am, David Mark wrote:
On Aug 1, 12:46 am, Peter Michaux wrote:On Jul 31, 6:35 pm, David Mark wrote:On Jul 31, 7:43 pm, Richard Cornford wrote:17. A runtime error.
var anObjectReference;
function outerFunction(){
function innerFunction() {
with(anObjectReference){
x = 5; //<--- The subject line of code.
}
}
innerFunction()
}
outerFunction();
I believe the error here isn't coming from the subject line
of code but the line before it.
Correct. That's one I missed due to not following instructions
to the letter.
Except the letter reads "Assuming the line that reads - x = 5; - is executed" and an error in the previous line cannot have happened if the line is executed.
A runtime error could still occur due to the subject line of
JavaScript. Taking it to the absurd, I have no clue what the
programmer who embedded JavaScript in a particular application
has done with the global x property. It may be that setting
the global x property always results in a runtime error.
I don't quite follow you there.
The assignment can produces a runtime error when the object being assigned to is a host object and it throws an exception when the assignment is made to an 'x' proeperty. Because nothing precludes the possibility that - anObjectReference - is a host object nothing precludes the possibility that the assignment will error.
In addition, on IE if no global 'x' variable is declared but an element exists in the DOM with the ID attribute "x" then IE creates an 'x' proeprty of the global object and assigns it a reference to the DOM element. Subsequent attempts to assign to the 'x' property of the global object then throw exceptions. So if no objects on the scope chain have 'x' properties the exception is thrown when - x = 5 - is resolved as an assignment to the 'x' property of the global object, and in the event that the global object were assigned to - anObjectReference - the same exception whould be thrown with both of - x = 5; - and - var x = 5; -.
<snip>I figure I failed, but this seems more of an academic exercise
than a practical test (and I am no expert on the guts of
ECMAScript.) This is reinforced by the fact that I haven't
used a single with clause in ten years of scripting Web
pages/applications. I've exploited closures once
Holy cow! What about setting scope for an event, XHR or timeout
callback with objects? Never anything like this...
Yes, I have done that last one recently (technically with
setInterval.) The other thing I use closures for is to associate
DOM events with objects, which is the tip I got from Richard's
article on the subject. But it should be mentioned that that
particular technique will cause a memory leak in IE unless you
clean up after it when the page unloads.
No, the code in that article does not produce a memory leak on IE. IE's memory leak problem has nothing to do with closures as such, it is to do with circular chains of reference that include JS objects and COM object. The assigning of the returned inner function form that example to an intrinsic event property of a DOM node does result in the DOM node having an indirect reference to a JS object, but so long as the JS object does not then hold a reference to the DOM node the circle is not closed and no garbage collection issues follow. The reason that the code passes a reference to - this - on to the method of the JS object called is so that the JS object code can have a reference to the DOM node when it needs one, but does not have to keep one and so provoke the memory leak issue.
On of the problems with mantra "closures cause memory leaks on IE" is that it brushes over the fact that circular references can be established without closures, and doing so is trivial and inherent in many commonly promoted scripting practices. Another case where knowing what you are doing is infinitely better than listening to some vague general advice on scripting.
Richard.
.
- Follow-Ups:
- Re: JavaScript knowledge test
- From: David Mark
- Re: JavaScript knowledge test
- References:
- Re: JavaScript knowledge test
- From: Richard Cornford
- Re: JavaScript knowledge test
- From: David Mark
- Re: JavaScript knowledge test
- From: Peter Michaux
- Re: JavaScript knowledge test
- From: David Mark
- Re: JavaScript knowledge test
- Prev by Date: Re: Using % on a table width, is it possible to get the width in pixel size?
- Next by Date: Re: JavaScript knowledge test
- Previous by thread: Re: JavaScript knowledge test
- Next by thread: Re: JavaScript knowledge test
- Index(es):
Relevant Pages
|