Re: JavaScript knowledge test



On Aug 1, 7:04 pm, "Richard Cornford" <Rich...@xxxxxxxxxxxxxxxxxxx>
wrote:
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.

Isn't that what I said? My example got it wrong as it caused an error
in the wrong line.


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.

Okay.


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

That's odd.

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

That's stranger still. Suffice to say that you wouldn't use the code
in this test in a production environment as it would be a nightmare to
maintain.


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.

<snip>

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

Right. I was being over-cautious in cleaning up all such events on
unload. In practice they often do create circular references (eg the
object has an "element" property that references the element with the
attached event), but not always.

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

Right.

passes a reference to - this - on to the method of the JS object called

Interesting that you mention that. I recall that I had some
difficulty getting that part to work as expected (though perhaps I
wasn't expecting the right thing.)

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.

That makes sense.


On of the problems with mantra "closures cause memory leaks on IE" is

That is no mantra of mine.

that it brushes over the fact that circular references can be
established without closures, and doing so is trivial and inherent in

Right. It just so happens that the cited example of attaching DOM
events to object methods uses closures and is always one line of code
away from establishing a memory leak in IE.

.



Relevant Pages

  • Re: JavaScript knowledge test
    ... 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. ... 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. ... 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. ...
    (comp.lang.javascript)
  • Re: OO Javascript for AJAX encapsulation
    ... function reference or an expression returning function ... In the second case we are creating variable "foo" and assigning ... In both cases a function object is created and a reference to ... that function is not assigned to the "foo" property until the assignment ...
    (comp.lang.javascript)
  • Re: pass by reference
    ... The only reason this doesn't work as they think, is because assignment ... because the pointer is what val *is*. ... The parameter is a reference, ... language that is entirely pass by value. ...
    (comp.lang.java.programmer)
  • Re: Assigning to references
    ... > want my reference member to refer to a new object; ... thus an assignment to it calls the copy assignment operator of T. ... creates a reference which binds to bobject. ... Use a pointer instead of a reference. ...
    (comp.lang.cpp)
  • Re: How to understand this form (something) (param);
    ... The code pattern queried doesn't necessarily create any closures. ... to the identifier tmp. ... In the first case, it's during execution of ... assignment is made so tmp stays undefined. ...
    (comp.lang.javascript)