Re: Add and object or a function?
- From: "Richard Cornford" <Richard@xxxxxxxxxxxxxxxxxxx>
- Date: Sun, 13 Nov 2005 01:29:58 -0000
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.
.
- Follow-Ups:
- Re: Add and object or a function?
- From: RobG
- Re: Add and object or a function?
- References:
- Add and object or a function?
- From: RobG
- Add and object or a function?
- Prev by Date: Re: Object Hash vs. object Array preference
- Next by Date: Re: Object Hash vs. object Array preference
- Previous by thread: Add and object or a function?
- Next by thread: Re: Add and object or a function?
- Index(es):
Relevant Pages
|