@@ -181,8 +181,9 @@ function TodoList() {
181181*/
182182
183183// ******************************
184- // Function Expressions ******************************
184+ // Function Expressions ******************************
185185// Example 14
186+ /*
186187console.log('\n')
187188// this on it's own is not valid, we can't call this
188189// We have no way of referring to this function
@@ -215,3 +216,53 @@ console.log(product(1, 99))
215216console.dir(add)
216217console.dir(sum)
217218console.dir(product)
219+ */
220+
221+ // Higher Order Functions ******************************
222+ // Example 15
223+
224+ console . log ( '\n' )
225+ // Function statement
226+ function add ( x , y ) {
227+ return x + y
228+ }
229+
230+ const subtract = function ( x , y ) {
231+ return x - y
232+ }
233+
234+ function multiply ( x , y ) {
235+ return x * y
236+ }
237+
238+ const divide = function ( x , y ) {
239+ return x / y
240+ }
241+
242+ const operations = [ add , subtract , multiply , divide ]
243+ console . log ( operations [ 0 ] )
244+ console . log ( operations [ 1 ] )
245+ console . log ( operations [ 2 ] )
246+ console . log ( operations [ 3 ] )
247+ console . log ( '\n' )
248+
249+ console . log ( operations [ 0 ] ( 100 , 4 ) ) // 104
250+ console . log ( operations [ 1 ] ( 100 , 4 ) ) // 96
251+ console . log ( operations [ 2 ] ( 100 , 4 ) ) // 400
252+ console . log ( operations [ 3 ] ( 100 , 4 ) ) // 25
253+
254+ console . log ( '\n' )
255+ // We can loop functions in an array
256+ for ( let func of operations ) {
257+ let result = func ( 30 , 5 )
258+ console . log ( result )
259+ }
260+
261+ console . log ( '\n' )
262+ // We can store functions in an object
263+ const thing = {
264+ // Method: a function stored in an object
265+ doSomething : multiply ,
266+ }
267+ console . log ( thing ) ;
268+ console . log ( thing . doSomething ( 50 , 2 ) ) // 100
0 commit comments