From: Josh Russo on
I'm a little confused about how best to handle user objects that
manage multiple DOM objects.

I was reading https://developer.mozilla.org/en/a_re-introduction_to_javascript
and it seems like my preferred pattern seems like it's exactly what
I'm not suppose to do.

function Widget(){
this.myfield = $('#id_myfield');
var clickHandler = function(event){ //do something };
this.myfield.bind('click', clickHandler);
}

myWdgt = new Widget();

It seems like this is the proper way to handle the issue is this:

function Widget(){
var clickHandler = function(event){ //do something };
(function(){
var myfield = $('#id_myfield');
myfield.bind('click', clickHandler);
})();
}

The only problem with this is that I don't get to cache the query of
MyField. Do I really need to re-query MyField in every method that
needs to use it? It just seems like so much more processing. Though if
that's the only way to avoid the memory leaks then so be it.

The other possibility I see is to capture the window.unload event to
manually destroy my objects. If I do this, is it enough to just
destroy my Widget instances or do I need to destroy the DOM references
individually within each Widget instance?

Thanks so much for your help.

Josh
From: Josh Russo on
Ok, lets start over.

First off I know exactly what jQuery is doing behind the scenes. The
object that it creates extends the array class. Each element of said
array is a DOM object. All additional jQuery helper methods are added
to the extended array class. Are you saying that because the DOM
elements are elements of a fancy array that it's no longer a circular
reference? That doesn't sound right. Maybe if these array elements
were not publicly accessible, but they are, so it sees that there
would still be a circular reference using a jQuery object as opposed
to a direct DOM element.

I guess the crux of my difficulty is that I want to avoid multiple
calls to methods like gEBI or $(). I think you hit my solution with
this bit:

function clickListener() {
// Do something
}

function Widget() {
var el = document.getElementById('id_myfield');
el.onclick = clickListener;
}

I just got too wrapped up in having everything encapsulated in the
class/function.

Thanks for talking me through that.
From: Erwin Moller on
Josh Russo schreef:
>
> By the way, what in the world do you mean by a cargo cult?
>

http://en.wikipedia.org/wiki/Cargo_cult_programming

which is based on Richard Feynman original:

http://en.wikipedia.org/wiki/Cargo_cult_science

The other issues I rather leave to David to answer. ;-)

Regards,
Erwin Moller



--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
From: David Mark on
On Jul 21, 6:20 am, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...(a)spamyourself.com> wrote:
> Josh Russo schreef:
>
>
>
> > By the way, what in the world do you mean by a cargo cult?
>
> http://en.wikipedia.org/wiki/Cargo_cult_programming
>

http://jquery.com/
http://www.prototypejs.org/
http://www.dojotoolkit.org/
http://www.sencha.com/

:)
From: Thomas 'PointedEars' Lahn on
Josh Russo wrote:

> First off I know exactly what jQuery is doing behind the scenes.

You think you do, but you don't. For not even its author(s) do(es). We've
be over this ad nauseam. You would be well-advised to read before you post.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)