Skip to content

Commit

Permalink
Fixes varios.
Browse files Browse the repository at this point in the history
  • Loading branch information
ftarulla committed May 12, 2016
1 parent af16e47 commit 728af59
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/arrow.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ In JavaScript, the invocation context (`this` value) is bounded when the functio
>
>JavaScript: The Definitive Guide (ch.8.2)
That is why in some situations `this` could be *lost*:
That is why in some situations, `this` may appear to be *something else*:

```js
var Timer = function() {
Expand All @@ -48,9 +48,9 @@ var Timer = function() {
var timer = new Timer();
timer.start(2000);
```
In this example, the timer displays `undefined`. And that's the correct behaviour ... although we want it to do something else! And the mistake arises with the position of `this` in the code, ie. its lexical position, and the fact that `this` is the **invocation context**.
In this example, the timer (after 1 second) displays `undefined`. And that's the **correct** behaviour ... although we want it to do something else! And the mistake arises with the position of `this` in the code, ie. its lexical position, and the fact that `this` is the **invocation context**.

The solution to this *problem* was to declare a new variable, assign the context to that variable and then, thanks to JavaScript closures (JavaScript: The Definitive Guide, ch.8.6), the variable (ie. the context that we want) can be accessed when the function is invoked
The solution to this *binding problem* is to declare a new variable, assign the context to that variable and then, thanks to JavaScript closures (JavaScript: The Definitive Guide, ch.8.6), the variable (ie. the context that we want) can be accessed when the function is invoked

```js
var TimerFixed = function() {
Expand All @@ -69,7 +69,7 @@ var timerFixed = new TimerFixed();
timerFixed.start(2000);
```

Now in ECMAScript6 we could use an **arrow function** to avoid this situation:
Now in ECMAScript6 we could use an **arrow function**:
```js
var Timer2 = function() {
return {
Expand All @@ -84,5 +84,7 @@ var Timer2 = function() {
var timer2 = new Timer2();
timer2.start(1000);
```
As we can see in the above example, the arrow function bound `this` to the enclosing context, ie. now we have a lexical this.

## Lexical arguments
*work in progress ...*

0 comments on commit 728af59

Please sign in to comment.