tc39 / proposal-bind-operator

This-Binding Syntax for ECMAScript
1.74k stars 30 forks source link

use arrow functions to create class methods with bind operation #48

Open stvkoch opened 7 years ago

stvkoch commented 7 years ago

Maybe this is out of topic, but still, belongs to bind operator subject.

Why don't use arrow functions operation to create methods of classes with bind operators?

class Example {
  bindFunc(...args) => { // automatic bind with "Example" object
    ...
  }
}

It's the same of:

class Example {
  bindFunc = (...args) => { // ugly way
    ...
  }
}
ljharb commented 7 years ago

This definitely doesn't relate to the bind operator, or to class properties - and I don't see why your last example is "ugly" either.

If you'd like to suggest a new change to the language, https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md may be helpful.

stvkoch commented 7 years ago

Semantically speaking... arrow functions define functions... not methods of classes.

Using the "ugly" way your code smell more like using workaround that a definition solution, strictly speaking

Alxandr commented 7 years ago

Strictly speaking, javascript only has functions. Not methods.

stvkoch commented 7 years ago

:) Strictly speaking about new ECMAScript standard library sorry

Now we support class and methods definitions, static variables,.. using syntactic sugar. I think, make sense we have one way to define methods of class with bind operators

mockdeep commented 7 years ago

@Alxandr I'm not sure what you mean by there are no methods. There are functions that bind this as the object that they're being called on, which sounds like more or less the definition of a method if you ask me.

I would definitely like to see a method syntax for binding scope. The second example above is pretty confusing to read. Is it assigning a global? A local? No, it's assigning a member of the class... The first actually seems to have a consistent interface with the current javascript method syntax with an arrow to indicate lexical binding.

Alxandr commented 5 years ago

@mockdeep well, no. There are functions that bind this to a lexical scope (ie, this is whatever it was where the function was defined). It's not related to the object they are being called on at all (that's how normal functions in JS work), rather it's dependent on the scope where the function expression got executed. At the end of the day, it's basically just allowing this to be bound by closures (just like any other variable in JS).