From 9cb35c02c3152d297f8286258f10962a8eef0d93 Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Wed, 12 May 2021 12:27:54 +1000 Subject: [PATCH] Module2: Demo: Using This Keyword --- ...roving_readability_with_arrow_functions.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/2_improving_readability_with_arrow_functions.md b/2_improving_readability_with_arrow_functions.md index e1aa6fa..33c0887 100644 --- a/2_improving_readability_with_arrow_functions.md +++ b/2_improving_readability_with_arrow_functions.md @@ -60,4 +60,29 @@ to let greeting = name => 'Hello ' + name; let message = greetings('John'); console.log(message); +``` + +## Bahavior of `this` Keyword + +`this` of regular function: +* refers to the owner of the function we are executing + * for standalone function, `this` refers global window obj + * for function within object, `this` refers the object + +`this` of arrow function +* Unlike regular functions, arrow functions do not have their own `this` value + +### Demo + +```javascript +let message = { + name: 'John', + regularFunction: function() { + console.log(this); + }, + arrowFunction: () => console.log(this) +} + +message.regularFunction(); // {name: "John", regularFunction: f, arrowFunction: f} +message.arrowFunction(); // {window: Window, self: Window, document: document, name: "", ...} ``` \ No newline at end of file