Tarek Ziadé wrote:
Tarek,
you only need to use a class if you want to pass some arguments to the
constructor otherwise use a literal object like
var CPSMystuff = {...}
Besides, your pattern creates memory leaks!, if you instanciate a new
object or a class just to simulate a namespace, your browser will run
out of memory sooner or later.
?
Your pattern of creating namespaces using classes creates memory leaks,
I'll post a demo, so you can see for yourself..
think about what happens when you do:
var CPSMyStuff = Class.create();
CPSMyStuff.prototype = { buttonClick: function () {
...
}
}
var my_stuff = new CPSMyStuff(); <==== put that in a loop and what
your browser die...
my_stuff.buttonClick()
:)
why do you want to put that in a loop ?
sorry i don't get that point,
put a loop over any instanciation and it consumes memory, yes.
The browser's garbage collector is called when the page is closed or
reloaded. So as long as you run the application on a same page, it is as
if you
were inside a loop. Putting the code inside a loop helps you identify
memory leaks as the ones you have just created.
Jean-Marc,
i think you are mixing for this particular use case,
between the fonction concept, the class concept, and the namespace concept,
the objective is not to create one instance of a given class type for
just one function.
I know, you could add as many functions in the example that I gave as
you want, there would be no difference, but before that, look again how
prototype.js or scriptaculous are written: top-level namespaces are
object literal, then inside these objects (Form, Element, ..) you have
functions, and classes, variables.
so it's not even as you say it is.
like i said you can hook javascript code any way you want. but we should
try to think o-o a bit here, for reusability, on the top of prototype
framework.
yes, but not everything needs to be a class either, for instance in
Element.getStyle(obj), Element is not a class because it doesn't need to
be, it is a collection of object that are classes or function, but the
object corresponding top the 'Element' namespace needs not be a class.
good example of classes are Effects, Widgets, ... because they
correspond to individual instances that have a identity of their own.
the o-o way would be to ask ourselves: where would the buttonClick
function fit the best ?
in OO way you should first have an object.
is it a simple, independant function ? or is it related to an object ?
could it be added in a class that gathers all functions aimed to
interact with forms, with the
whole container ?
you need an object first. In some cases when it makes no sense to
associate functions to classes, you will simply place them under a same
object's namespace, as it's the case in prototype's Position object
(which gives some information about the position of an object on the
page). The reason Position is not a class is because you'd run out of
memory very fast if instead of writing:
var position = Position.page(element)
you wrote (as you've already suggested)
var positionInfo = new Position(element);
var position = positionInfo.page()
think about a drag-and-drop situation for instance.
can it evolve and start to interact with other functions ? etc...
I know, I know, but that's not the point.
I think we can discuss all this with the material i am doing in the
incoming CPS javascript guideline.
thanks for this feedback, i really think the guideline will be improved
through this kind of talks.
Tarek
what about the memory leaks?
/JM
_______________________________________________
cps-devel mailing list
http://lists.nuxeo.com/mailman/listinfo/cps-devel