473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Add and object or a function?


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?

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

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.

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, or is it just a preference for a
particular coding style?
1.
<URL:http://www.litotes.demon.co.uk/example_scripts/tableHighlighter.html>
--
Rob
Nov 10 '05 #1
3 1332
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.
Nov 23 '05 #2
Richard Cornford wrote:
RobG wrote: [...]
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.


That's as I thought - it has ramifications for re-use that affect the
overall architecture of an application.

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


Hmm ... I don't know enough about such things yet. I thought one of the
good things about using a browser and open standards was to get away
from a lot of the underlying implementation issues, but I guess
expecting it to be fully realised is a bit of a pipe-dream.
[...]
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.


I had seen the same style from YEP. The bottom line being use what is
most appropriate for the circumstance - the catch being knowing enough
to make the decision (or even what the options are).

Thank you for the reply.
--
Rob
Nov 23 '05 #3
RobG wrote:
Richard Cornford wrote: <snip>
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


Hmm ... I don't know enough about such things yet. I thought
one of the good things about using a browser and open standards
was to get away from a lot of the underlying implementation
issues, but I guess expecting it to be fully realised is a bit
of a pipe-dream.


We are talking about a browser bug, and a long-standing one. It is
becoming increasingly significant because people are trying to do ever
more on web browsers, and so inevitably on IE. The whole AJAX idea is
predicated on not changing pages in a browser so garbage collection
issues (and particularly bugs) seem the issue of the moment.
[...]
Is Richard's way clearly better,
<snip> I had seen the same style from YEP.
Credit where it is due, Yep was the first person that I observed using
the inline execution of a function expression. Having seen that, and
realising that it was compliantly reasonable and valid in javascript, I
noticed that it could be applied to extending Douglas Crockford's
technique for private instance members to provide private static members
(using a similar emulation). Once Yep had seen the application he picked
it up and ran with it a bit.
The bottom line being use what is most appropriate for
the circumstance - the catch being knowing enough
to make the decision (or even what the options are).


There is a lot of judgement involved, and many opinions. I like
encapsulation because it can eliminate the possibility of accidental
interactions. That is a good thing in a large and complex system and a
total irrelevance in a score of lines of code on a web page.

Richard.
Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.