Skip to content

Commit 86ac274

Browse files
Add Arrow Function lexical arguments example
Credits to @empyrical at lukehoban/es6features#87
1 parent 5193d57 commit 86ac274

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

docs/learn-es6.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ for full specification of the ECMAScript 2015 language.
4747
Arrows are a function shorthand using the `=>` syntax. They are syntactically
4848
similar to the related feature in C#, Java 8 and CoffeeScript. They support
4949
both expression and statement bodies. Unlike functions, arrows share the same
50-
lexical `this` as their surrounding code.
50+
lexical `this` as their surrounding code. If an arrow is inside another function,
51+
it shares the "arguments" variable of its parent function.
5152

5253
```js
5354
// Expression bodies
@@ -69,6 +70,22 @@ var bob = {
6970
console.log(this._name + " knows " + f));
7071
}
7172
};
73+
74+
// Lexical arguments
75+
function square() {
76+
let example = () => {
77+
let numbers = [];
78+
for (number of arguments) {
79+
numbers.push(number * 2);
80+
}
81+
82+
return numbers;
83+
};
84+
85+
return example();
86+
}
87+
88+
square(2, 4, 7.5, 8, 11.5, 21); // returns: [4, 8, 15, 16, 23, 42]
7289
```
7390

7491
### Classes

0 commit comments

Comments
 (0)