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

Allow this inside the body if no explicit name provided #3

Closed
hax opened this issue Jun 15, 2016 · 2 comments · Fixed by #4
Closed

Allow this inside the body if no explicit name provided #3

hax opened this issue Jun 15, 2016 · 2 comments · Fixed by #4

Comments

@hax
Copy link
Contributor

hax commented Jun 15, 2016

Update: function(this) {} already be supported by TS from 2016, also supported by Closure compiler (search "function this type"), and also have PR in Flowtype.

Original comment:

This is the example of current proposal:

function method (this elem, e) {
  console.log("Elem:", elem) // OK
  console.log("this:", this) // <-- Throws an error!
}

I suggest allow this inside the body if no explicit name provided, so we can write:

function method (this, e) {
  console.log("this:", this) // <-- That's ok
}

Though it looks redundant, it will be useful for type annotation (See microsoft/TypeScript#6739 and facebook/flow#452 (comment)), and linter (See http://eslint.org/docs/rules/no-invalid-this and http://eslint.org/docs/rules/prefer-arrow-callback#allowunboundthis).


Update: I changed my mind that this part should be leave to linters.

Original comments:

And current proposal allow this in inner functions:

runTask(function cb (this elem, e) {

  elem.runAsyncTask(function () {
    console.log("Outer elem:", elem) // OK
    console.log("Inner this:", this) // OK
  });
})

I suggest do not allow it, but require adding this in the arguments:

runTask(function cb (this elem, e) {

  elem.runAsyncTask(function () {
    console.log("Outer elem:", elem) // OK
    console.log("Inner this:", this) // Syntax error
  });
  elem.runAsyncTask(function (this) {
    console.log("Outer elem:", elem) // OK
    console.log("Inner this:", this) // OK
  });
})
@gilbert
Copy link
Owner

gilbert commented Nov 23, 2019

Allowing for no explicit name seems good to me.

Wouldn't making it required break backwards compatibility?

@hax
Copy link
Contributor Author

hax commented Nov 25, 2019

As I understand, it won't break backwards compatibility. this in parameter position is syntax error now. For example new Function('this', '') throw SyntaxError.

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

Successfully merging a pull request may close this issue.

2 participants