Using Object Literals as Classes

David Bruant bruant.d at gmail.com
Fri Mar 16 15:30:06 PDT 2012


Le 16/03/2012 23:12, Rick Waldron a écrit :
> On Fri, Mar 16, 2012 at 6:04 PM, David Bruant <bruant.d at gmail.com
> <mailto:bruant.d at gmail.com>> wrote:
>
>     Le 16/03/2012 23:00, Rick Waldron a écrit :
>>     On Fri, Mar 16, 2012 at 5:12 PM, Domenic Denicola
>>     <domenic at domenicdenicola.com
>>     <mailto:domenic at domenicdenicola.com>> wrote:
>>
>>         Just to contribute to this... er... fun-thread...
>>
>>         My team uses the closure pattern for our "classes" (i.e. no
>>         prototype methods at all), since we value encapsulation. I
>>         can't imagine we're alone.
>>
>>
>>     For my own curiosity, can you point me to some examples where you
>>     are strategically avoiding the use of the prototype pattern?
>     When he needs actual encapsulation.
>     Unfortunately, methods on prototype require to have properties
>     that are public.
>
>
>     If you avoid prototype methods, all your attributes and private
>     methods can be shared by public method scopes.
>
>
> Sorry, I don't subscribe to this as an adequate argument against
> prototypes. jQuery has a whole lot of hidden, private functions and
> data - using an IIFE. Ultimately, the developer makes the decision to
> write well encapsulated code - prototype or closure pattern should
> have no bearing.
I agree that it works with one prototype object, but it doesn't scale.
The problem does not come from either prototype inheritance nor
encapsulation, but trying to have them both (and several layers of
inheritance).

If I want to create my own library "on top" of jQuery and that my
objects have the standard jQuery API + my own functions, I can, but I
won't have access to the internals of jQuery. I'm restricted to the
public API, which can be a limitation. Java has "protected" that allows
a subclass to have access to the internals of another class to solve
this issue.
I can also add my things to $.fn... but... $.fn.galery is probably taken
by half a billion libraries already :-)

I intuit that with private name, it will be possible to have
encapsulation and several layer of inheritance.


With current JavaScript, you can have the sort of
encapsulation+inheritance that you want without private names, but it
will take ugly code using weakmaps (or a good ES5-compatible polyfill of
them).

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120316/1c77c2cb/attachment-0001.html>


More information about the es-discuss mailing list