Re: Add and object or a function?



RobG wrote:
> I am playing with a script that will allow columns of a table
> to be moved by dragging them left or right. To do this with
> functions is fairly straight forward, however after looking at
> Richard Cornford's version of Table Highlighter[1] I decided
> to do it using an object.
>
> Richard's basic code structure is:
>
> var SomeObject = (function()
> {
> function someFn01(){
> // statements
> // return something
> }
> function setOnEvent01(arg1, arg2){
> return (function() {
> // statements
> });
> }
> return (function(arg1, arg2, argN){
> // use someFn01() & setOnEvent01()
> })
> })();
>
>
> Are there any particular benefits to this style? It
> encapsulates all the functions and variables within a single
> object, but are there any others?

Encapsulation is almost the entire point of the exercise. It can provide
very slight performance gains in some environments, but not to the
extent that this approach is worth pursuing for that reason.

> Will this expose IE's memory leak problem so that I need
> to use Richard's finalizeMe() or something similar?

Anything that produces a circular reference chain that includes a COM
object (so IE's DOM nodes and other objects) will produce IE's memory
leak. Closures produce less obvious circular references, if you don't
appreciate what you are doing. But an action as simple as ginning an
Element an ID and then any intrinsic event attribute will theoretically
produce a circular reference (The ID causes a global property to refer
to the Element, the Element refers to the resulting intrinsic event
handling function, and the event handling function refers to the global
object on its scope chain). Given how easy it is to produce memory leaks
on IE I would rather explicitly create those circular references, know
that I am doing it and consequentially handle the explicit breaking of
those references.

However, it should be noted that some closure-based approaches can
actually avoid circular references where they may otherwise be required.
This is particularly true for the creation of event handlers as return
values from functions, as the handlers can refer to their Element as -
this - and access the object through the closure, so the object itself
does not have to hold on to a reference to the Element, it can be passed
that reference when the event handler fires.

> I intend using this with an onload function that passes a
> class name that's used to get references to all the tables
> that can be re-ordered with drag 'n drop. It will probably
> attach mousedown/move/up events to the tables, get and keep
> references to all the columns and their positions and other
> stuff like indicate which column is being dragged, where it
> will go when dropped, etc.

That all seems reasonable, and can be implemented in many ways.

> My alternative is to have a constructor that creates an
> object for each table to hold data relevant to each table
> (e.g. references to columns) and add methods to the object
> prototype for moving the columns.
>
> Is Richard's way clearly better,

No. 'Better' cannot be reasonably assessed without a context to provide
criteria for the judgement. Your context would suggest an OO approach
but not necessarily any particular style of OO javascript.

> or is it just a preference for a
> particular coding style?

In context it was an experiment with a particular style of coding. It
worked well enough to produce a well-encapsulated and clearly defined
object. My thinking has moved on a bit since then but it is still a
style that I use when it seems appropriate.

Richard.


.



Relevant Pages

  • Re: IDE dies during compile
    ... > fairly sure there are no circular references or EVERYONE would be having ... Yeah everyones cpu spikes/bursts for this kind of request, i was more interested in sustained ... If it falls over before it even hits ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Memory Leak IE Javascript attribute add onblur
    ... and breaking circular chains of references usually involves ... It is better to break only the circular references you mad in your ... DOM leaks go away. ... Ajax is pretty, but always seems to cause more problems than it's ...
    (comp.lang.javascript)
  • Re: Object Inspector
    ... you must be creating the object the property refers ... My real issue is when some/all these references are *circular ... having circular references isn't a bad thing ... I have been able to do this with Jedi's JvInspector and it works ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Re-enable disabled form elements when user goes Back
    ... circular references. ... references of course and you avoid adding expandos to elements. ... need to improve on it as I don't often remove listeners or attach lots ... all depend on a context of course. ...
    (comp.lang.javascript)
  • Re: Setting variables to Nothing
    ... >immediately before it was automatically set to nothing was ... >or lost references, and the only solution is to avoid problems ... >Circular references can be broken by setting the references ... This is a coding practice. ...
    (microsoft.public.access.modulesdaovba)