|
| 1 | +## Different ways of declaring functions |
| 2 | + |
| 3 | +- Functions are **First class citizens:** |
| 4 | + |
| 5 | + - It means JavaScript functions are values in themselves. They can be stored in variables and passed into other functions, just like any other piece of data in JavaScript. (see 3rd example below) |
| 6 | + |
| 7 | +- **With function keyword** |
| 8 | + |
| 9 | + _Important:_ These are **hoisted**, meaning JavaScript 'hoists' or puts them at the top of the file. So if we try to run a function defined with function keyword before it is defined/ above its definition, there's no error and the function is executed successfully. |
| 10 | + |
| 11 | + ```javascript |
| 12 | + function doctorize(firstName) { |
| 13 | + return `Dr. ${firstName}`; |
| 14 | + } |
| 15 | + ``` |
| 16 | + |
| 17 | +- **Anonymous function - function with no name** |
| 18 | + |
| 19 | + These are used in _callbacks_ and _IIFE: immediately invoked function expressions_. |
| 20 | + |
| 21 | + ```javascript |
| 22 | + function (firstName) { |
| 23 | + return `Dr. ${firstName}`; |
| 24 | + } |
| 25 | + |
| 26 | + ``` |
| 27 | + |
| 28 | +- **Function Expression** |
| 29 | + |
| 30 | + _Important:_ These are **not hoisted**, meaning JavaScript doesn't put them at the top of the file. So if we try to run a function not defined with function keyword before it is defined/ above its definition, there's an error and the function fails to execute. |
| 31 | + |
| 32 | + ```javascript |
| 33 | + const doctorize = function(firstName) { |
| 34 | + return `Dr. ${firstName}`; |
| 35 | + }; |
| 36 | + ``` |
| 37 | + |
| 38 | +- **Arrow Functions** |
| 39 | + |
| 40 | + - Concise, shorter syntax |
| 41 | + |
| 42 | + - Don't have own scope in refer to 'this' keyword |
| 43 | + |
| 44 | + - Are anonymous functions |
| 45 | + |
| 46 | + ```javascript |
| 47 | + const inchToCM = inches => { |
| 48 | + return inches * 2.54; |
| 49 | + }; |
| 50 | + |
| 51 | + // Implicit return: const add = (a, b = 3) => a + b; const inchToCM = (inches) => inches * 2.54; |
| 52 | + |
| 53 | + // In case of only 1 parameter, we can omit the () around it const inchToCM = inches => inches * 2.54; |
| 54 | + ``` |
| 55 | + |
| 56 | + - Implicitly returning an object: |
| 57 | + |
| 58 | + ```javascript |
| 59 | + const makeABaby = (first, last) => ({ name: `${first} ${last}`, age: 0 }); |
| 60 | + ``` |
| 61 | + |
| 62 | +- **IIFE: Immediately Invoked Function Expression** |
| 63 | + |
| 64 | + ```javascript |
| 65 | + (function(age) { |
| 66 | + return `You are cool and age ${age}`; |
| 67 | + })(10); |
| 68 | +
|
| 69 | + // Parantheses run first in JavaScript, so we have wrapped the function in (). |
| 70 | + // The function immediately runs. |
| 71 | + // The argument passed here is 10 for parameter age. |
| 72 | + ``` |
| 73 | + |
| 74 | +- **Methods:** |
| 75 | + |
| 76 | + - A function which lives inside an object. |
| 77 | + |
| 78 | + - For e.g `console.log('hey')` : here `log` is the function and `console` is the object. |
| 79 | + |
| 80 | + - **2 Ways to define methods:** |
| 81 | + |
| 82 | + ```javascript |
| 83 | + const wes = { |
| 84 | + name: "Westopher Bos", |
| 85 | + // Method! |
| 86 | + sayHi: function() { |
| 87 | + console.log(`Hey ${this.name}`); |
| 88 | + return "Hey Wes"; |
| 89 | + }, |
| 90 | + // Arrow function |
| 91 | + wisperHi: () => { |
| 92 | + console.log("hii wesss im a mouse"); |
| 93 | + }, |
| 94 | + // Short hand Method |
| 95 | + yellHi() { |
| 96 | + console.log("HEY WESSSSS"); |
| 97 | + } |
| 98 | + }; |
| 99 | + ``` |
| 100 | + |
| 101 | +- **Callback functions:** |
| 102 | + |
| 103 | + - Function that is passed to another function used for something that will happen when something is done. |
| 104 | + |
| 105 | +``` |
| 106 | +HTML: |
| 107 | + <body> |
| 108 | + <button class="clickMe"> |
| 109 | + Click Me! |
| 110 | + </button> |
| 111 | + </body> |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + Javascript: |
| 118 | + // Click Callback const button = document.querySelector('.clickMe'); |
| 119 | + |
| 120 | + function handleClick() { console.log('Great Clicking!!'); } |
| 121 | + |
| 122 | + button.addEventListener('click', handleClick); // everytime we click the button, the handleClick function is run. |
| 123 | + |
| 124 | + button.addEventListener('click', function() { console.log('Nice Job!!!'); }); // everytime we click the button, the function inside is run. |
| 125 | + |
| 126 | + // Timer Callback setTimeout(() => { console.log('DONE! Time to eat!'); }, 1000); // it runs the console.log after each 1 sec or 1000 milliseconds |
| 127 | +``` |
0 commit comments