Skip to content

Commit

Permalink
Add Arrow Function lexical arguments example (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
PandaWhisperer authored and hzoo committed Oct 5, 2016
1 parent a011e28 commit bcceef1
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion docs/learn-es6.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ for full specification of the ECMAScript 2015 language.
Arrows are a function shorthand using the `=>` syntax. They are syntactically
similar to the related feature in C#, Java 8 and CoffeeScript. They support
both expression and statement bodies. Unlike functions, arrows share the same
lexical `this` as their surrounding code.
lexical `this` as their surrounding code. If an arrow is inside another function,
it shares the "arguments" variable of its parent function.

```js
// Expression bodies
Expand All @@ -69,6 +70,22 @@ var bob = {
console.log(this._name + " knows " + f));
}
};

// Lexical arguments
function square() {
let example = () => {
let numbers = [];
for (number of arguments) {
numbers.push(number * 2);
}

return numbers;
};

return example();
}

square(2, 4, 7.5, 8, 11.5, 21); // returns: [4, 8, 15, 16, 23, 42]
```

### Classes
Expand Down

0 comments on commit bcceef1

Please sign in to comment.