Function Context Cheatsheet

Types of Invocation

Function-style invocation

Method-style invocation

const obj = {
  name: 'Example Object',
  unnamedFunc: function() {
    console.log(this.name);
  }
};

// Method-style invocation
obj.unnamedFunc(); // 'Example Object' 

// Function-style invocation
const unnamedFunc = obj.unnamedFunc;
unnamedFunc(); // Global context

Types of Functions

Named Function

function namedFunc(params) {
  return 'named function'
}
// bindedNamedFunc will have the context of obj
const bindedNamedFunc = namedFunc.bind(obj);

Unnamed Function

const unnamedFunc = function(params) {
  return 'unnamed function'
}
// bindedUnnamedFunc will have the context of obj
const bindedUnnamedFunc = unnamedFunc.bind(obj);

Explicit Fat Arrow Function

const explicitFatArrow = params => {
  return 'explicit fat arrow function'
};

Implicit Fat Arrow Function

const implicitFatArrow = (params) => 'implicit fat arrow function';
const implicitFatArrow = (params) => ('implicit fat arrow function');
const implicitFatArrow = (params) => ({
  function: 'implicit fat arrow'
});

Bind