| .apply() | The apply() method calls a function with a given this value, and arguments provided as an array (or an array-like object). |
| .bind() | returns copy of function where this is set to the first argument passed into .bind() |
| .call() | The call() method calls a function with a given this value and arguments provided individually. |
variable declarations && function declaration, hoisted or moved to the top of the file
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
{% code title="Function definition" %}
function functionName(parameter = 'default value') { //scope start
const functionBody = 'do something';
return functionBody; //return statement
} //end scope
const capturedVal = functionName(0, arguments);{% endcode %}
{% code title="Regular function declaration" %}
function doctorize(firstName) {
return `Dr. ${firstName}`;
}{% endcode %}
{% code title="Anon function" %}
function (firstName) {
return `Dr. ${firstName}`;
}{% endcode %}
{% code title="Function expression" %}
const doctorize = function (firstName) {
return `Dr. ${firstName}`;
}{% endcode %}
{% code title="Arrow function" %}
const inchesToCM = inches => inches *2.54;
//function inchToCM(inches) {
// const cm = inches * 2.54;
// return cm;
//}{% endcode %}
{% code title="IIFE - immediately invoked function expression" %}
(function() {
console.log('Running the Anon function');
return;
})();{% endcode %}
{% code title="Methods!!!" %}
const wes = {
name: 'Wes Bos',
//Method!
sayHi: function() {
console.log('Hey Wes');
return 'Hey Wes';
},
//Shorthand method
yellHi() {
console.log('HEY WESSSSS');
},
//arrow function
whisperHi: () => {
console.log('heeyyy wess');
}
}{% endcode %}
{% code title="Callback Functions" %}
//Click Callback
const button = document.querySelector('.clickMe');
function handleClick() {
console.log('Great Clicking!!!');
}
button.addEventListener('click', handleClick);
//Timer Callback
setTimeout(function() {
console.log('DONE! Time to eat!!');
}, 1000);{% endcode %}
{% code title="High order Function" %}
async function go() {
const pizza = await makePizza(['pineapple']).catch(handleDisgustingPizza);
return pizza;
}
// catch it at run time
go().catch(handleError);
// make a safe function with a HOF
function makeSafe(fn, errorHandler) {
return function () {
fn().catch(errorHandler)
}
}
const safeGo = makeSafe(go, handleError);
safeGo();{% endcode %}
