|
| 1 | +# Chapter 1 |
| 2 | + |
| 3 | +#Javascripts: |
| 4 | + |
| 5 | +var - The scope of a variable declared with the keyword var is its current execution context. |
| 6 | +Let - let are block scoped and not function scoped. |
| 7 | + |
| 8 | +#Hoisting functions |
| 9 | + |
| 10 | +Function declarations |
| 11 | + |
| 12 | +hoisted(); // Output: "This function has been hoisted." |
| 13 | + |
| 14 | +function hoisted() { |
| 15 | + console.log('This function has been hoisted.'); |
| 16 | +}; |
| 17 | + |
| 18 | +Function expressions |
| 19 | + |
| 20 | +Function expressions, however are not hoisted. |
| 21 | + |
| 22 | +expression(); // Ouput: TypeError: expression is not a function |
| 23 | + |
| 24 | +var expression = function hoisting() { |
| 25 | + console.log('Will this work?'); |
| 26 | +}; |
| 27 | + |
| 28 | + |
| 29 | +https://scotch.io/tutorials/understanding-hoisting-in-javascript |
| 30 | + |
| 31 | +async / await |
| 32 | +promises |
| 33 | +callbacks |
| 34 | + |
| 35 | +#Async/await |
| 36 | + |
| 37 | +Async/await is a new way to write asynchronous code. Previous options for asynchronous code are callbacks and promises. |
| 38 | +Async/await is actually built on top of promises. It cannot be used with plain callbacks or node callbacks. |
| 39 | +Async/await is, like promises, non blocking. |
| 40 | +Async/await makes asynchronous code look and behave a little more like synchronous code. This is where all its power lies. |
| 41 | + |
| 42 | +const makeRequest = () => |
| 43 | + getJSON() |
| 44 | + .then(data => { |
| 45 | + console.log(data) |
| 46 | + return "done" |
| 47 | + }) |
| 48 | + |
| 49 | +makeRequest() |
| 50 | + |
| 51 | +const makeRequest = async () => { |
| 52 | + console.log(await getJSON()) |
| 53 | + return "done" |
| 54 | +} |
| 55 | + |
| 56 | +makeRequest() |
| 57 | + |
| 58 | +#closures |
| 59 | + |
| 60 | +A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: |
| 61 | + - it has access to its own scope (variables defined between its curly brackets). |
| 62 | + - it has access to the outer function’s variables. |
| 63 | + - it has access to the global variables. |
| 64 | + |
| 65 | +Closure means that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned. |
| 66 | + |
| 67 | +Inner function can access variables and parameters of an outer function. |
| 68 | +It is useful in hiding implementation detail in JavaScript. |
| 69 | + |
| 70 | +function showName (firstName, lastName) { |
| 71 | +var nameIntro = "Your name is "; |
| 72 | + // this inner function has access to the outer function's variables, including the parameter |
| 73 | +function makeFullName () { |
| 74 | +return nameIntro + firstName + " " + lastName; |
| 75 | +} |
| 76 | + |
| 77 | +return makeFullName (); |
| 78 | +} |
| 79 | + |
| 80 | +showName ("Michael", "Jackson"); // Your name is Michael Jackson |
| 81 | + |
| 82 | +function OuterFunction() { |
| 83 | + |
| 84 | + var outerVariable = 100; |
| 85 | + |
| 86 | + function InnerFunction() { |
| 87 | + alert(outerVariable); |
| 88 | + } |
| 89 | + |
| 90 | + return InnerFunction; |
| 91 | +} |
| 92 | +var innerFunc = OuterFunction(); |
| 93 | + |
| 94 | +innerFunc(); // 100 |
| 95 | + |
| 96 | + |
| 97 | +function Counter() { |
| 98 | + var counter = 0; |
| 99 | + |
| 100 | + function IncreaseCounter() { |
| 101 | + return counter += 1; |
| 102 | + }; |
| 103 | + |
| 104 | + return IncreaseCounter; |
| 105 | +} |
| 106 | + |
| 107 | +var counter = Counter(); |
| 108 | +alert(counter()); // 1 |
| 109 | +alert(counter()); // 2 |
| 110 | +alert(counter()); // 3 |
| 111 | +alert(counter()); // 4 |
| 112 | + |
| 113 | +In the above example, return InnerFunction; returns InnerFunction from OuterFunction when you call OuterFunction(). A variable innerFunc reference the InnerFunction() only, not the OuterFunction(). So now, when you call innerFunc(), it can still access outerVariable which is declared in OuterFunction(). This is called Closure. |
| 114 | + |
| 115 | +#IIFE |
| 116 | + |
| 117 | +IIFE is a function expression that automatically invokes after completion of the definition. |
| 118 | +The parenthesis () plays important role in IIFE pattern. |
| 119 | +use () operator to call this anonymous function immediately after completion of its definition. |
| 120 | +(function () { |
| 121 | + //write your js code here |
| 122 | +})(); |
| 123 | + |
| 124 | +Adcvantages: |
| 125 | + |
| 126 | +Do not create unnecessary global variables and functions |
| 127 | +Functions and variables defined in IIFE do not conflict with other functions & variables even if they have same name. |
| 128 | +Organize JavaScript code. |
| 129 | +Make JavaScript code maintainable. |
| 130 | + |
| 131 | +#this(): |
| 132 | + |
| 133 | +The following four rules applies to this in order to know which object is referred by this keyword. |
| 134 | + |
| 135 | +Global Scope |
| 136 | +Object's Method |
| 137 | +call() or apply() method |
| 138 | +bind() method |
0 commit comments