[thelist] JS: createHTMLElement and onclick

Matt Warden mwarden at gmail.com
Fri Jul 7 12:43:06 CDT 2006


-----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?

- --
Matt Warden
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFErp0qrI3LObhzHRMRAuXIAJ0a+RdeyQM3H1CdjC3+FyWAXIr0OgCfZQv7
Tq6A40rJiRc7qnOmsjQKOM0=
=OFnC
-----END PGP SIGNATURE-----



More information about the thelist mailing list