defining classes - perferred approach

M

mca

this is a bit long-ish post regarding class defnitions in javascript.

i am interested to know how most folks prefer to define classes in
javascript. i know there are a handful of very good 'frameworks' for
handling oo-type js code. i am currently interested in exploring the
internal details of javascript/ecmascript in this matter, tho.

i have seen two primary approaches for defining classes in javascript.
for the sake of argument i will call them "enclosed" and "prototype"
here's an example:

// enclosed approach
someClass = function()
{
this.methodName = function()
{
return "methodName";
}
}

// prototype approach
anotherClass = function()
{
}
anotherClass.prototype.methodName = function()
{
return "methodName";
}

i find that the choice of this approach has implications for
'overriding' patterns. for example, if you use the "enclosed" approach,
you cannot inherit from the class and then use the "prototype" approach
to override the base class method. however, you *can* inherit from the
the base class and use the same "enclosed" approach to successfully
override the method.

i also find that the "enclosed" method is very friendly when dealing
with private variables of the class, but that the "prototype" approach
is less so.

i am interested in other's views on these two approaches. are there
traps here? are there other, more reliable approaches than these? is
one better suited for publicly-shared libraries and another better for
personal/internal use?

tia for your feedback.

mca
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top