Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stvkoch opened this issue May 22, 2017 · 6 comments
Open

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

stvkoch opened this issue May 22, 2017 · 6 comments

Comments

@stvkoch
Copy link

stvkoch commented May 22, 2017

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
Copy link
Member

ljharb commented May 22, 2017

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
Copy link
Author

stvkoch commented May 23, 2017

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

Alxandr commented May 23, 2017

Strictly speaking, javascript only has functions. Not methods.

@stvkoch
Copy link
Author

stvkoch commented May 23, 2017

:) 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
Copy link

@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
Copy link

Alxandr commented Jan 15, 2019

@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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants