> Anyway, prototypes are good and object literals are good. I think a lot of
> the class love is about syntax: they are tidy and self-evident. Object
> literals are also tidy and self-evident. Prototypes are just messy as they
> stand in JS. I've taken to enjoying a pattern where you can use object
> literals to define objects and still get inheritance using prototypes. You've
> probably seen this type of thing before:
>
> var Person = Animal.inherit({
> getName: function() {
> return this.name;
> }
> });
>
> var bob = Person.inherit({
> name: 'Bob'
> });
>
> bob.getName(); // Bob
>
> I have some code to provide this type of pattern here
> https://github.com/incompl/super-inherit/blob/master/demo.js
>
> But what I'm really getting at is that I think if JavaScript made it this
> neat and tidy to do prototypal inheritance there might be less clamoring for
> classes.
I agree. Note that you can do the above already with the <| operator. You just
can’t use new Person() to create new instances:
var Person = Animal <| {
getName: function() {
return this.name;
}
};
var bob = Person <| {
name: 'Bob'
};
bob.getName(); // Bob
I still like tremendously that the same mechanism can be used for “subclassing”
and for creating an instance directly.
> Also, I think having super would help a lot.
It seems to be a done deal for ES.next and is not tied to classes!
--
Dr. Axel Rauschmayer
[email protected]
home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss