Skip to content

Commit

Permalink
explainer/extensions: Remove mentions of .apply
Browse files Browse the repository at this point in the history
See #12.
  • Loading branch information
js-choi committed Oct 25, 2021
1 parent a84e959 commit 5314119
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
31 changes: 10 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,20 @@ so `receiver::(x?.prop)` is also not useful.)
## Why a bind-this operator
In short:

1. [`.bind`][bind], [`.call`][call], and [`.apply`][apply]
1. [`.bind`][bind] and [`.call`][call]
are very useful and very common in JavaScript codebases.
2. But `.bind`, `.call`, and `.apply` are clunky and unergonomic.
2. But `.bind` and `.call` are clunky and unergonomic.

[bind]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
[call]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
[apply]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

### `.bind`, `.call`, and `.apply` are very common
### `.bind` and `.call` are very common
The dynamic `this` binding is a fundamental part of JavaScript design and practice today.
Because of this, developers frequently need to change the `this` binding.
`.bind`, `.call`, and `.apply` are arguably three of the most commonly used functions
`.bind` and `.call` are arguably three of the most commonly used functions
in all JavaScript.

We can estimate `.bind`, `.call`, and `.apply`’s prevalences using [Node Gzemnid][].
We can estimate `.bind` and `.call`’s prevalences using [Node Gzemnid][].
Although [Gzemnid can be deceptive][], we are only seeking rough estimations.

[Node Gzemnid]: https://github.com/nodejs/Gzemnid
Expand All @@ -177,7 +176,7 @@ of the top-1000 downloaded NPM packages.
| 168,872 |`.set` |
| 70,116 |`.push` |

These results suggest that usage of `.call`, `.bind`, and `.apply`
These results suggest that usage of `.bind` and `.call`
are comparable to usage of other frequently used standard functions.
In this dataset, their combined usage even exceeds that of `console.log`.

Expand All @@ -187,7 +186,7 @@ relative to other baseline functions.
Gzemnid counts each library’s codebase only once; it does not double-count dependencies.

In fact, this method definitely underestimates the prevalences
of `.bind`, `.call`, and `.apply`
of `.bind` and `.call`
by excluding the large JavaScript codebases of Node and Deno.
Node and Deno [copiously use bound functions for security][security-use-case]
hundreds or thousands of times.
Expand Down Expand Up @@ -224,7 +223,7 @@ grep -aE "\.call\b"
```

We use `awk` to count those matching lines of code
and compare their numbers for `bind`, `call`, `apply`,
and compare their numbers for `bind`, `call`,
and several other frequently used functions.

```bash
Expand All @@ -233,8 +232,6 @@ search.topcode.sh
slim.topcode.1000.txt.lz4
> ./search.topcode.sh '\.call\b' | grep -E --invert-match '//.*\.call|/\*.+\.call|[^a-zA-Z][A-Z][a-zA-Z0-9_$]*\.call\( *this|_super\.call|_super\.prototype\.|_getPrototypeOf|_possibleConstructorReturn|__super__|WEBPACK VAR INJECTION|_objectWithoutProperties|\.hasOwnProperty\.call' | awk 'END { print NR }'
500084
> ./search.topcode.sh '\.apply\b' | awk 'END { print NR }'
225315
> ./search.topcode.sh '\.bind\b' | awk 'END { print NR }'
170248
> ./search.topcode.sh '\b.map\b' | awk 'END { print NR }'
Expand Down Expand Up @@ -288,13 +285,13 @@ for the purposes of rough comparison.

</details>

### `.bind`, `.call`, and `.apply` are clunky
### `.bind` and `.call` are clunky
JavaScript developers are used to using methods in a [noun–verb–noun word order][]
that resembles English and other [SVO human languages][]: `receiver.method(arg)`.

[SVO human languages]: https://en.wikipedia.org/wiki/Category:Subject–verb–object_languages

However, `.bind`, `.call`, and `.apply` flip this “natural” word order,
However, `.bind` and `.call` flip this “natural” word order,
They flip the first noun and the verb,
and they interpose the verb’s `Function.prototype` method between them:
`method.call(receiver, arg)`.
Expand All @@ -315,14 +312,6 @@ match = self::formatter(val);
createDebug.formatArgs.call(self, args);
self::createDebug.formatArgs(args);

// [email protected]/build-es5/index.js
var code = fn.apply(colorConvert, arguments);
var code = colorConvert::fn(...arguments);

// [email protected]/q.js
return value.apply(thisp, args);
return thisp::value(...args);

// [email protected]/src/internal/operators/every.ts
result = this.predicate.call(this.thisArg, value, this.index++, this.source);
result = this.thisArg::this.predicate(value, this.index++, this.source);
Expand Down
13 changes: 6 additions & 7 deletions extensions-comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ However, several of these points are debatable.
but clunky. We should improve their ergonomics with syntax.”

Both proposals agree with this statement.
[`.bind`, `.call`, and `.apply` are very common][common],
[`.bind` and `.call` are very common][common],
but [they are also very clunky][clunky].

2. “Extracting get/set accessors is also important, so it should get syntax too.”
Expand Down Expand Up @@ -775,9 +775,9 @@ However, several of these points are debatable.

In contrast, the [bind-`this` operator][bind-this] is focused on **one problem**:

1. [`.bind`][bind], [`.call`][call], and [`.apply`][apply]
1. [`.bind`][bind] and [`.call`][call]
are very **useful** and very **common** in JavaScript codebases…
2. …but `.bind`, `.call`, and `.apply` are **clunky** and **unergonomic**.
2. …but `.bind` and `.call` are **clunky** and **unergonomic**.

All the other issues that the extensions system solves
is either solved by the [pipe operator][]
Expand All @@ -791,7 +791,7 @@ However, it has little hope of advancing in TC39 as it is.
And it tries to solve more problems than necessary.

But there is still a real need for a syntax
that makes [`.bind`][bind], [`.call`][call], and [`.apply`][apply]
that makes [`.bind`][bind] and [`.call`][call]
less clunky and more ergonomic.

A single, simple [bind-`this` operator][bind-this] without extra features
Expand All @@ -816,6 +816,5 @@ and does not carry a risk of ecosystem schism.
[security use case]: https://github.com/js-choi/proposal-bind-this/blob/main/security-use-case.md
[bind]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
[call]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
[apply]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
[common]: https://github.com/js-choi/proposal-bind-this/blob/main/README.md#bind-call-and-apply-are-very-common
[clunky]: https://github.com/js-choi/proposal-bind-this/blob/main/README.md#bind-call-and-apply-are-clunky
[common]: https://github.com/js-choi/proposal-bind-this/blob/main/README.md#bind-and-call-are-very-common
[clunky]: https://github.com/js-choi/proposal-bind-this/blob/main/README.md#bind-and-call-are-clunky

0 comments on commit 5314119

Please sign in to comment.