Skip to content

Commit

Permalink
explainer § Why: Stub other justifications
Browse files Browse the repository at this point in the history
See #8.
  • Loading branch information
js-choi committed Sep 30, 2021
1 parent 04e7527 commit cdfc0c3
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,30 @@ is equivalent to `a->(createFn())`.
There are **no other special rules**.

## Why a bind-`this` operator

### `call` and `bind` are very common and should be easier
[`Function.prototype.bind`][call] and [`Function.prototype.call`][bind]
are very common in **object-oriented JavaScript** code.
They are useful methods that allows us to apply functions to any object,
are very common in **object-oriented JavaScript** code;
they could even be considered core parts of its design.

[TODO]

[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

### Tree-shakable methods should be easier
They allow developers to apply functions to any object,
binding their first arguments to the `this` bindings within those functions,
no matter the current object environment.
`bind` and `call` allow us to **extend** an **object** with a function
as if that function were **its own method**.
They serve as an important link between
the **object-oriented** and **functional** styles in JavaScript.

[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
[TODO]

Why then would we need an operator that does the same thing?
Because `bind` and `call` are vulnerable to **global mutation**.
### Protection against global mutation should be easier
`Function.prototype.bind` and `call` are vulnerable to **global mutation**.

For example, when we run our code in an untrusted environment,
an adversary may mutate global prototype objects
Expand Down

0 comments on commit cdfc0c3

Please sign in to comment.