In case of passing primitive type (e.g. a string or a number) to the function, the argument is a copy of the variable value, and not the original variable.
But, when a reference type is passed (e.g. an object or an array), the argument is a reference to the object in the memory heap.
So, it works just same as copying the variable outside of the function.
JavaScript does not having passing by references, only passing by value, even in reference types case, in which it passes a value to the reference and not the reference itself.
First-Class vs. Higher-Order Functions
First-class Functions
JavaScript treats functions as first-class citizens. (language feature)
This means that functions are simply values.
Functions are just another Functions are just another “type” of object.
Functions can be stored in variables or properties.
Functions can be passed as arguments to other functions.
We can return functions from functions.
We can also call methods on functions. counter.inc.bind(someOtherObject);
Higher-order Functions
A function that receives another function as an argument, that returns a new function, or both.
This is only possible because of first-class functions.
Abstraction in Programming with First Class Functions
JavaScript uses first class functions and a higher order functions to implement callback functions to allow us to create abstractions
We can create a higher level logic by hiding things that a function does that does not need to be shown. We can do this by having functions that can then be used as callback functions in another function.
A ‘higher level’ function will not care on how the argument it passes works. We can just use another function in it, by abstracting and thus delegating functions to other ‘lower’ level functions.
This is important in OOP to ensure that the code base is more focused.
Code Examples
Functions Accepting Callback Functions
Functions Returning Functions
Call and Apply
Function methods explicitly set this keyword by setting the object that function will be calling with.
They do the same thing, but the difference is that call accepts additional arguments, while apply doesn’t receive a list of arguments after this keyword, but instead it takes an array of arguments.
Apply method is less used in modern JavaScript, because we can do the same thing with call and spread operator.
Bind
Just like call and apply, bind allows to manually set this keyword for any function call, the difference is that it doesn’t immediately call the function but instead, it returns a new function where this keyword is bound.
We can also pass additional arguments to bind, then we call the new method it will called with these arguments. (partial application)
So, to use a function that requires this with an event listener we need to use bind.
Partial Application
Partial application means pre-setting the function parameters.
bind can be used for partial application without the need for setting this by simply setting it to null.
Immediately Invoked Function Expressions (IIFE)
IIFE
Sometimes in JavaScript, we need a function that disappears right after it’s called once).
This can be done by writing the function expression without assigning it to any variable then wrapping all of this into parentheses (transforming the statement into an expression).
It’s useful for encapsulating data, since inner scope won’t be accessed by any outer one.
This technique will be needed with async/await.
In modern JavaScript IIFE is not used widely because blocks can be used for achieving the same behavior with variables.
Arrow functions are just a simpler way of writing anonymous functions, they produce a lot cleaner code.
Function declarations can be hoisted, meaning you can call the function before it is declared in JavaScript. Arrow functions on the other hand can only be called after they have been declared.
You can’t use arrow functions when declaring methods in an object. Inside an object, if you have a method you need to use a proper anonymous function declaration.
You can reduce and simplify the arrow function syntax to the point where it becomes really hard to understand what’s going on (just the parameter and the arrow and it points directly as the output).
Arrow functions and this
If you’re using ‘this’ in a method within an object, and you then get an odd result, try turning the function into an arrow function to see if that solves the problem. Most likely, you’re dealing with the wrong scope and an arrow function will help you get the correct scope because it doesn’t carry its own scope with it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#arrow_functions
Pass data to a function with parameters
Return values from a function
Callbacks
There’s a good chance when you set up some more complex code that you may have different callback functions you want to use for different purposes.
So in this particular circumstance we want to use the print HTML function but there could be several different versions of the print HTML function and then you want to use them for different purposes and by calling a callback like this, you can pass in exactly the function you want into the other function.
So we’re effectively saying here is the precise function I want you to use once you’re done processing your information right now but later it could be a different function.