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
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