Module 2: Writing Arrow Functions
parent
95550ca928
commit
f6de87bb39
63
2_improving_readability_with_arrow_functions.md
Normal file
63
2_improving_readability_with_arrow_functions.md
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
# Module 2: Improving Readability with Arrow Functions
|
||||||
|
|
||||||
|
Objectives:
|
||||||
|
* Arrow Functions - What and Why
|
||||||
|
* Defining Arrow Functions
|
||||||
|
* Behavior of `this` Keyword
|
||||||
|
|
||||||
|
## Introducing Arrow Functions
|
||||||
|
|
||||||
|
Feature of Arrow Functions
|
||||||
|
* Introduced in ES6
|
||||||
|
* Simpler way to write a function expression
|
||||||
|
* Why to use them?
|
||||||
|
* shorter syntax
|
||||||
|
* `this` derives its value from enclosing [lexical scope](https://stackoverflow.com/questions/1047454/what-is-lexical-scope)
|
||||||
|
|
||||||
|
## Writing Arrow Functions
|
||||||
|
|
||||||
|
### Function 1
|
||||||
|
Transfer Regular Function 1
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
let greetings = function() { // anonymous function
|
||||||
|
return 'Hello World!';
|
||||||
|
};
|
||||||
|
let message = greetings();
|
||||||
|
console.log(message); // Hello World!
|
||||||
|
```
|
||||||
|
|
||||||
|
to Arrow Function
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
let greeting = () => {
|
||||||
|
return 'Hello World!';
|
||||||
|
};
|
||||||
|
let message = greetings();
|
||||||
|
console.log(message);
|
||||||
|
```
|
||||||
|
|
||||||
|
to Simplified Arrow Function
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
let greeting = () => 'Hello World!';
|
||||||
|
let message = greetings();
|
||||||
|
console.log(message);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Function2
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
let greet = function greetings(name) {
|
||||||
|
return 'Hello ' + name;
|
||||||
|
}
|
||||||
|
console.log(message);
|
||||||
|
```
|
||||||
|
|
||||||
|
to
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
let greeting = name => 'Hello ' + name;
|
||||||
|
let message = greetings('John');
|
||||||
|
console.log(message);
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user