From f6de87bb39e317445e5b4469c1cae2a656e651d3 Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Wed, 12 May 2021 12:07:55 +1000 Subject: [PATCH] Module 2: Writing Arrow Functions --- ...roving_readability_with_arrow_functions.md | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 2_improving_readability_with_arrow_functions.md diff --git a/2_improving_readability_with_arrow_functions.md b/2_improving_readability_with_arrow_functions.md new file mode 100644 index 0000000..e1aa6fa --- /dev/null +++ b/2_improving_readability_with_arrow_functions.md @@ -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); +``` \ No newline at end of file