Richard Cornford wrote:
> Christopher J. Hahn wrote:
> > Richard Cornford wrote:
> >> Christopher J. Hahn wrote:
> >>> If I'm right, I believe that's because functions are
> >>> primitive data types in JS, like strings and numbers,
> >>
> >> Functions are object in javascript. And like all other
> >> objects they can have named properties added to them,
> >> and assigned values, at any time.
> >>
> >> > so they're passed "by value" in all operations, and
> >> > not by reference.
> >> <snip>
> >>
> >> Being objects they are passed by reference only.
> >>
> >> Richard.
> >
> > Not to be argumentative,
>
> Don't worry about being argumentative.
Courtesy is a pretense with a purpose.
> > but if they are passed by reference only then could you
> > explain why the referant isn't freed, causing an error in
> > the case described by Razzbar?
>
> Superficial testing is the most likely explanation of the behaviour
> described. The bottom line is that calling a function that has been
> defined in a frame that has unloaded via a reference to that function in
> another frame is not going to work cross-browser. Confidence acquired by
> only testing browsers where it does work is no indicator of reliability.
>
> Consider what would happen where it does work. Think about garbage
> collection. If the containing frame refers to a function that originates
> in (and so in some sense belongs to) another frame's global object then
> that function should not be able to be garbage collected when the
> contents of that other frame unload. And as the function's scope chain
> refers to the global object then that global object should not be
> garbage collectable, and as the global object refers to the document
> (and indirectly to its contents) the document should not be garbage
> collectable, and so on. So where it works it is a brilliant way of tying
> up a big chunk of memory in exchange for the ability to use what is
> potentially a tiny function. (And might even result in talk of memory
> leaks, where the memory consumption would in reality be the expected
> consequences of behaviour being specifically programmed).
But there are other functionalities inherent in the language itself
that allow for remote creation of objects without resorting to the
ill-considered and clumsy implementation implied by the behavior to
which you're referring. There is no reason for a JS implementor to make
garbage collection quirky.
> However, the execution environment of an unloaded frame is (to some
> degree or another, in different browsers) dismantled and functions and
> objects referred to by properties in different frames may start
> producing errors if used. that is certainly the case for functions in
> some versions of Windows IE 6, and if it doesn't work in Windows IE 6
> then it has no potential application in a commercial project.
>
> That this should happen, and that the results should differ considerably
> between browsers (and browser versions) is not particularly surprising
> as ECMAScript is designed for one global object, and one execution
> environment, rather than multiple interacting environments. There is
> simply no specified mechanism for the interactions between environments.
There's no reason the window object need be considered the top-level
object internally, so there's no reason for implementors to consider
the window to be an environment as opposed to another object in the
scope chain.
The problem seems to stem from the ambiguity of the relationship
between a document and its window. It would seem more intuitive (to me,
anyway) that variables and objects created by a document should be
properties of the document, rather than the window. Again, this is
obviously not the language's fault as (to my knowledge), the language
itself doesn't provide window or document objects.
This would be another point wehere I would like to see the W3C weigh
in.
> > I've been too busy to actually test the case, mind you,
> > but I'm fairly sure he's correct.
>
> Do try it.
>
> Richard.
The ability to do things that I believe I should not expect to work is
something that I try to forget about, as it lends itself to bad
practice in other languages and/or past or future versions or other
implementations of the same language (like I mentioned with PERL's
automatic return of the value of the last expression in a function). To
me, thought should be just as portable as code.
Thanks for that excellent answer, Richard.
Sorry if any of what I've said makes little sense-- I've been pretty
swamped and my mind is scattered across many things at once. This week
I am thinking in PERL::threads+C. Next week I'll be thinking in
PHP/C+HTML again. The week after it'll be JS+PHP+NNTP+HTML, with some
MySQL on the side.
|