[thelist] JS: createHTMLElement and onclick

ben morrison morrison.ben at gmail.com
Mon Jul 10 03:57:00 CDT 2006


On 7/7/06, Matt Warden <mwarden at gmail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> ben morrison wrote:
> >> However, you may also want to support passing a function reference, as
> >> you could avoid large memory use when creating many elements with the
> >> same onclick.
> >
> > hmm not sure what you mean, am kind of at the stretch of my knowledge here
>
> You can pass references to functions. e.g.:
>
> function callit(func)
> {
>     callit();
> }
>
> function somefunction()
> {
>    // stuff
> }
>
> callit(somefunction);
> callit(somefunction);
> callit(somefunction);
> callit(somefunction);
>
> somefunction (without the parens) refers to the function object, and you
> can pass it around like any other variable/reference. The example above
> is nonsense, but it demonstrates what I'm talking about.
>
> If you do it this way, there is only one copy of somefunction in memory.
> Contrast this to:
>
> callit(function()
> {
>    // stuff
> });
> callit(function()
> {
>    // stuff
> });
> callit(function()
> {
>    // stuff
> });
> callit(function()
> {
>    // stuff
> });
>
> 4 copies of the same function. This is similar to your situation. If you
> call your function which then does new Function(argument) each time, and
> argument is the same in each time, you are unnecessarily using memory.
>
> Does that make it any clearer?

A lot clearer thankyou, think it's time to buy christians book :)

Ben



More information about the thelist mailing list