Skip to content

Instantly share code, notes, and snippets.

@AnimeshPandey
AnimeshPandey / 1_this_Example_11.js
Last active February 28, 2021 00:03
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 11. Implicit versus new
function catchPokémon(name) {
this.name = name;
}
var firstAttempt = {
catchPokémon : catchPokémon
};
var secondAttempt = {};
@AnimeshPandey
AnimeshPandey / 1_this_Example_12.js
Created February 28, 2021 00:03
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"