Concise Method Binding

Gilbert B Garza gilbertbgarza at gmail.com
Wed Nov 11 20:19:21 UTC 2015


Ah ok, I understand now; if you're using the `class` syntax, you want to be
able to arbitrarily and declaratively bind methods to the instance. Makes
sense.

I also prefer your suggestion to delegating this functionality to a
decorator; ideally the decorator would be built into the language.

On Wed, Nov 11, 2015 at 12:24 PM, Bergi <a.d.bergi at web.de> wrote:

> Gilbert B Garza schrieb:
>
>> Forgive me if I'm missing the point, but isn't the entire purpose of using
>> classes to make all instances share the same function in memory via `this`
>> ? If you want methods to always be bound, why not use closures instead?
>>
>> ```js
>> function Person (name) {
>>    var person = this
>>    person.greet = function () {
>>      return person.name + " says hi!";
>>    };
>> }
>>
>
> Yes, the point of prototypes is sharing. But maybe we don't want (cannot
> use) that.
> The point of the `class` syntax is just to enable a more declarative
> definition of class methods - which should include some way to create
> instance methods as well, simply because they're needed often enough.
>
> And being able to declare methods that will get bound outside of the
> constructor (even if that's a bit confusing) avoids much boilerplate again.
> Just compare
> ```js
> class Person extends … {
>   constructor(name) {
>     super(name);
>     this.greet = () => {
>       return this.name + " says hi!";
>     };
>   }
> }
> ```
> vs
> ```js
> class Person extends … { // with default constructor
>   ::greet() {
>     return this.name + " says hi!";
>   }
> }
> ```
>
> Regards,
>  Bergi
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151111/ddea75b9/attachment.html>


More information about the es-discuss mailing list