Skip to content

Commit b3aeb31

Browse files
committed
arrow functions, let & const, mdn links
1 parent 5535c6b commit b3aeb31

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

arrows/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# [Arrows functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
2+
3+
Arrows ```=>``` are a short hand way of writing a function. However there are a few differences between the two.
4+
5+
Firstly arrow functions are always anonymous so to create a function using an arrow we must assign it to a variable:
6+
7+
```
8+
let add1 = (number) => {
9+
number = number + 1;
10+
}
11+
```
12+
13+
The arrow allows for single line code which can improve readablility as well as reducing the amount of code that you need to write. Also with one argument you can remove the parenthesis around the argument.
14+
15+
```
16+
var add1 = number.map((number, i) => number + i);
17+
var add1 = number.map(number => number + 1);
18+
```
19+
Arrows also share the same [lexical ](http://whatis.techtarget.com/definition/lexical-scoping-static-scoping) ```this``` as their surrounding code which means that inherit the scope. However according to Douglas Crockford we shouldn't even be using this.

arrows/problems/01.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*In the snippet of code below please replace all functions
2+
with arrows and then run the file using npm run babel-node */
3+
4+
var arr = [1,2,3];
5+
6+
function arrMap (arr) {
7+
return arr.map( function (num) {
8+
return num + 1;
9+
})
10+
};
11+
12+
console.log(arrMap(arr));

let/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [Let](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/let) + [Const](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)
2+
3+
Up until now the only option for declaring a variable was by using the statement ```var``` which is scoped to the nearest function block. ```let``` on the other hand is scoped to the nearest enclosing block.
4+
5+
Although Douglas Crockford sees no issue with using var he notes that to developers coming from different language back grounds singling out function blocks is unusual. Therefor he proposes that we use let instead of var. However, the most important point is that pick one of the two and stick with it.
6+
7+
The ```const``` statement creates a read-only constant.

let/problems/01.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* try playing around with let const and var and if you
2+
find anything cool feel free to write an example and make a pr
3+
here are a few functions to start you off*/
4+
5+
6+
7+
let hello1 = (greeting) => {
8+
9+
if (greeting) {
10+
let say = greeting;
11+
}
12+
console.log(say);
13+
};
14+
15+
var hello2 = (greeting) => {
16+
17+
if (greeting) {
18+
var say = greeting;
19+
}
20+
console.log(say);
21+
};

0 commit comments

Comments
 (0)