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 explicit this parameter without renaming #4

Merged
merged 2 commits into from
Jan 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,32 @@
> `this` isn't really a keyword, it is the natural "main" function argument in JavaScript.
> - (@spion)[https://github.com/mindeavor/es-pipeline-operator/issues/2#issuecomment-162348536]
This proposal extends the `function` declaration syntax to allow for explicit naming of what is normally called `this`. Its primary use case is to play nicely with the [function bind proposal](https://github.com/zenparsing/es-function-bind), making such functions more readable. For example:
This proposal extends the `function` declaration syntax to allow for explicit naming of what is normally called `this`.

```js
Object.defineProperty(User.prototype, {
get: function fullName() { // original version
return `${this.firstName} ${this.lastName}`
},
configurable: true,
})

// versions use the feature of this proposal

function fullName(this) { // explicit this parameter
return `${this.firstName} ${this.lastName}`
}

function fullName(this user) { // explicit naming `this` to `user`
return `${user.firstName} ${user.lastName}`
}

function fullName(this {firstName, lastName}) { // destructuring
return `${firstName} ${lastName}`
}
```

Its primary use case is to play nicely with the [function bind proposal](https://github.com/zenparsing/es-function-bind), making such functions more readable. For example:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Its primary use case is to play nicely with the [function bind proposal](https://github.com/zenparsing/es-function-bind), making such functions more readable. For example:
The original motivation of this proposal was to play nicely with the [function bind proposal](https://github.com/zenparsing/es-function-bind), making such functions more readable. For example:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I'm planning revive :: notation and plan to present to committee on 2020 Feb meeting, I would like to keep the sentence. What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep the original, but you should also write about its newer applications. Here are a couple off the top of my head:

  • Less transpiling required (TypeScript supports this syntax and it makes sense)
  • Now that arrow functions are ubiquitous, a linter could require a named this for function declarations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the second point.


```js
function zip (this array, otherArray) {
Expand Down Expand Up @@ -64,6 +89,22 @@ function process (this obj, name) {
};
```

Explicit `this` parameter also allow type annotation or parameter decorators be added just like normal parameter.

```ts
// Type annotation (TypeScript, already possible today)
Number.prototype.toHexString = function (this: number) {
return this.toString(16)
}
```

```ts
// Parameter decorators (future proposal)
Number.prototype.toHexString = function (@toNumber this num) {
return num.toString(16) // same as Number(num).toString(16)
}
```

## Behavior

If a function explicitly names `this`, attempting to use `this` inside the body will throw an error:
Expand Down