-
The 10 days of JS - Higher Order functionsJavascript 2020. 5. 13. 05:14
// Lesson Overview // 1. Example of function that accepts a function as an arugument // 2. Create an example function that returns a function // 3. Useful higher-order functions that are part of the language itself (and not just web browser jargon) // * web browser jargon: 웹 브라우저 전문 용어 //1,2 //document.addEventListener('click', ourAmazingFunction) //we include parentheses to run or call the function and you might remember that it takes two arguments. So the first argument is which event you want to be on the lookout for. the second argument is supposed to be a function that you want to run when this event happens // function ourAmazingFunction() { // alert('Thank you for clicking') // } //The reason I'm showing you this is I want to point out what a higher order function is. //We would say that this addEventListener method or function is a higher order function because the second argument that it expects to receive is a function. // However in certain programming languages you are not allowed to pass a function as an argument. You can usually only pass simple values like a string of text or a number. // So javascript is special in the sense that functions are not considered weird or special entities with their own unique set of rules of where and how they can be used. // A higher-order function is a function that either: // (A) Accepts a function as an argument // (B) Returns a function (as a result) // not a higher order function. Clearly it does not accept a function as an argument. // //document.write(doubleMe(20)) //receive a number, does not return a function. returns a simple number function createMultiplier(multiplier) { return function(x) { return x * multiplier } } // our createMultiplier function is considered a higher order function because instead of just returning a simple string of text or a number or an array it's returning a function. let doubleMe = createMultiplier(2) // literally the double me variable is going to equal this function that gets returned here and ^ let tripleMe = createMultiplier(3) let quadrupleMe = createMultiplier(4) let stawberryCount = ['red', 'orange'] // in JavaScript a function is an entity just like any other value which also means that not only can we return a function within a function but we can also assign a function to a variable like we see up // this makes javascript a very flexible and powerful language. //document.write(quadrupleMe(10)) // 40 //^then we can call it just like any other function. // an example of a higher order function that the web browser environment offers //3 //we created our own functions. show you a few higher order functions that are part of the core javascript language itself. let myColors = ['pink','lightblue', 'green', 'black'] myColors.forEach(saysomethingNice) myColors.map() myColors.filter() function saysomethingNice(color) { document.write('The Color ' + color + ' is a great color.<br>') }
reference))
https://www.udemy.com/course/learn-javascript-full-stack-from-scratch/
'Javascript' 카테고리의 다른 글
The 10 days of JS - Scope & Context - 1 (0) 2020.05.15 The 10 days of JS - the difference between returning and mutating (0) 2020.05.15 The 10 days of JS - making decision (0) 2020.05.11 The 10 days of JS - Array (0) 2020.05.11 The 10 days of JS - Object (0) 2020.05.08