Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helpful example code to demonstrate the use of "this" in JavaScript for my article at https://anmshpndy.medium.com/how-well-do-you-know-this-ce4355bc9b
// Example 12. Explicit versus new through hard-binding
function catchPokémon(name) {
this.name = name;
}
var firstAttempt = {};
var attemptBinder = catchPokémon.bind(firstAttempt);
attemptBinder("Onix");
console.log(firstAttempt.name);
// Output : "Onix"
var secondAttempt = new attemptBinder("Steelix");
console.log(firstAttempt.name);
// Output : "Onix"
console.log(secondAttempt.name);
// Output : "Steelix"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment