functional programming
Sun Jul 07 2024 09:52:49 GMT+0000 (Coordinated Universal Time)
Saved by @NoFox420 #javascript
Functions are ideal for reusable logic. When a function itself needs to reuse logic, you can declare a nested function to handle that logic. Here is an example of a nested function: Example Code const outer = () => { const inner = () => { }; }; Object properties consist of key/value pairs. You can use shorthand property names when declaring an object literal. When using the shorthand property name syntax, the name of the variable becomes the property key and its value the property value. The following example declares a user object with the properties userId, firstName, and loggedIn. Example Code const userId = 1; const firstName = "John"; const loggedIn = true; const user = { userId, firstName, loggedIn, }; console.log(user); // { userId: 1, firstName: 'John', loggedIn: true } The concept of returning a function within a function is called currying. This approach allows you to create a variable that holds a function to be called later, but with a reference to the parameters of the outer function call. For example: Example Code const innerOne = elemValue(1); const final = innerOne("A"); innerOne would be your inner function, with num set to 1, and final would have the value of the cell with the id of A1. This is possible because functions have access to all variables declared at their creation. This is called closure. In your elemValue function, you explicitly declared a function called inner and returned it. However, because you are using arrow syntax, you can implicitly return a function. For example: Example Code const curry = soup => veggies => {}; curry is a function which takes a soup parameter and returns a function which takes a veggies parameter. You can pass a function reference as a callback parameter. A function reference is a function name without the parentheses. For example: Example Code const myFunc = (val) => `value: ${val}`; const array = [1, 2, 3]; const newArray = array.map(myFunc); The .map() method here will call the myFunc function, passing the same arguments that a .map() callback takes. The first argument is the value of the array at the current iteration, so newArray would be [value: 1, value: 2, value: 3]. Arrays have a .some() method. Like the .filter() method, .some() accepts a callback function which should take an element of the array as the argument. The .some() method will return true if the callback function returns true for at least one element in the array. Here is an example of a .some() method call to check if any element in the array is an uppercase letter. Example Code const arr = ["A", "b", "C"]; arr.some(letter => letter === letter.toUpperCase()); Arrays have an .every() method. Like the .some() method, .every() accepts a callback function which should take an element of the array as the argument. The .every() method will return true if the callback function returns true for all elements in the array. Here is an example of a .every() method call to check if all elements in the array are uppercase letters. Example Code const arr = ["A", "b", "C"]; arr.every(letter => letter === letter.toUpperCase());
Comments