Skip to content

Commit

Permalink
Lexical Arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ftarulla committed May 23, 2016
1 parent 728af59 commit e682bab
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
19 changes: 14 additions & 5 deletions bin/arrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var Timer = function () {
};
};
var timer = new Timer();
timer.start(1000);
timer.start(1);

var TimerFixed = function () {
return {
Expand All @@ -96,7 +96,7 @@ var TimerFixed = function () {
};
};
var timerFixed = new TimerFixed();
timerFixed.start(1000);
timerFixed.start(1);

var Timer2 = function () {
return {
Expand All @@ -109,9 +109,18 @@ var Timer2 = function () {
};
};
var timer2 = new Timer2();
timer2.start(1000);
timer2.start(1);

/*******************************************************************************
* And what about the arguments?
*
*/
*/
var foo = function () {
console.log("Arguments of foo:");
console.log(arguments); // { '0': 'hello', '1': 'world', '2': 123 }

(n => {
console.log("Arguments of inner anonymous function:");
console.log(arguments);
})(42);
};
foo('hello', 'world', 123);
17 changes: 13 additions & 4 deletions src/arrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var Timer = function() {
};
};
var timer = new Timer();
timer.start(1000);
timer.start(1);

var TimerFixed = function() {
return {
Expand All @@ -95,7 +95,7 @@ var TimerFixed = function() {
};
};
var timerFixed = new TimerFixed();
timerFixed.start(1000);
timerFixed.start(1);

var Timer2 = function() {
return {
Expand All @@ -108,10 +108,19 @@ var Timer2 = function() {
};
};
var timer2 = new Timer2();
timer2.start(1000);
timer2.start(1);


/*******************************************************************************
* And what about the arguments?
*
*/
var foo = function() {
console.log("Arguments of foo:");
console.log(arguments); // { '0': 'hello', '1': 'world', '2': 123 }

((n) => {
console.log("Arguments of inner anonymous function:");
console.log(arguments); // { '0': 'hello', '1': 'world', '2': 123 }
})(42);
}
foo('hello', 'world', 123);
22 changes: 20 additions & 2 deletions src/arrow.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ timer.start(2000);
```
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 *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
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 Down Expand Up @@ -87,4 +87,22 @@ 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 ...*
Being consistent with `this`, the arguments variable is a reference to the arguments' object of the enclosing scope. Let's see an example:
```js
var foo = function() {
console.log("Arguments of foo:");
console.log(arguments); // { '0': 'hello', '1': 'world', '2': 123 }

((n) => {
console.log("Arguments of inner anonymous function:");
console.log(arguments); // { '0': 'hello', '1': 'world', '2': 123 }
})(42);
}
foo('hello', 'world', 123);
```

##References
* __You-Dont-Know-JS__
https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20&%20beyond/ch2.md
* __Mozilla Developer Network__
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

0 comments on commit e682bab

Please sign in to comment.