thisInJavaScript

If can solve this exercise then you do understand `this` in JavaScript.

by Niranjan Borawake

JavaScript

const javaScriptMessage = {
  language: 'JavaScript',
  message: 'I owe you.'
};

function printMessage() {
  console.log(`Hey ${this.language}...! ${this.message}`)
}

let printJavaScriptMessage = printMessage.bind(javaScriptMessage);
printJavaScriptMessage(); // Hey JavaScript...! I owe you.

// Now let's assume we don't have `bind`
Function.prototype.bind = null;

// Below line will throw - Uncaught TypeError: printMessage.bind is not a function

// printJavaScriptMessage = printMessage.bind(javaScriptMessage);

// Can you try to implement `bind` and prove you understand `this` in JavaScript.

Function.prototype.bind = function () {
// Implementation goes here
}

// Hoping this works post implementation
printJavaScriptMessage = printMessage.bind(javaScriptMessage);
printJavaScriptMessage(); // Hey JavaScript...! I owe you.