"Richard A. DeVenezia" <radevenz@ix.netcom.com> writes:
[color=blue]
> So, then, is it possible to document.write html that would pass a function
> var to the handler
>
> i.e.
> function foo ()
> {
> ...
> var adjunctData = { a:1, b:2, c:3, ..., j:10 }
> document.write ('... <TD onmouseout="Out(this, adjunctData)" > text
> </TD>...')[/color]
[color=blue]
> ...
> }
> won't work since adjunctData would have to be global scope. I don't want to
> pollute global space.[/color]
Exactly.
This is actually similar to the problems you get when using strings
as arguments to setTimeout or using the Function constructor function.
The text that you write is "just a string", and cannot capture
the variables in the current scope the way a closure can.
What you can do, is to assign the onmouseout handler from Javascript
instead:
var adjunctData = { a:1, b:2, c:3, ..., j:10 }
document.write('<td id="foo"> .... </td>');
document.getElementById("foo").onmouseout =
function () {
Out(this,adjunctData);
};
This avoids Javascript creating more scripts as strings, which I
personlly don't like.
[color=blue]
>
> I don't think that
> document.write ('... <TD onmouseout="Out(this, ' + adjunctData + ')" > text
> </TD>...')
> would work either. If I recall in IE adjunctData works out to something like
> [Object] object[/color]
Yes. That would only work if the toString method returned a literal
that evaluates to the same value ... and that is equivalent to including
the entire object in each location.
[color=blue]
>
> document.write ('... <TD onmouseout="Out(this, ' + function () { return
> adjunctData } + ')" > text </TD>...')
> might work (will try soon)?[/color]
Probably not. The function is still converted to a string. That string
is then parsed again as Javascript, but in a different scope. The
scope is lost because you go through a string. Strings have no notion
of scope or evaluation context.
[color=blue]
> If so, at what number of 'outs' would any
> appreciable degradation in browser reponse/resource utilization occur?[/color]
That depends on the garbage collectior. The functions you create are
lost immediately, and can be garbage collected.
My bet is that if those functions cause a significant drain on
ressources, your page is too large to build with DHTML anyway.
[color=blue]
> adding and managing a function property of type array that holds references
> to the 'parametric' objects and pass the index into this array[/color]
....
This is simply the simplest way of not polluting the global namescape:
Create *one* global variable, put an object (e.g., a function) in it,
and store the rest of your variables in that object.
Building an array, and using an integer as index, is not different from
indexing an object by name ... it's just easier to come up with the
names :)
[color=blue]
> Of these is there a 'best of breed' technique, or is it more a matter of
> personal style ?[/color]
Personal style goes a long way :)
I would try to not write scripts. Javascript writing Javascript just
seems like a waste to me. Why build a string and the parse it again,
when I can just write Javascript directly.
/L
--
Lasse Reichstein Nielsen -
Join Bytes!
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'