Skip to content

Instantly share code, notes, and snippets.

@AnimeshPandey
Last active February 28, 2021 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnimeshPandey/d360a84e633b13e6fdbbe8dba0bcec01 to your computer and use it in GitHub Desktop.
Save AnimeshPandey/d360a84e633b13e6fdbbe8dba0bcec01 to your computer and use it in GitHub Desktop.
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 = {};
firstAttempt.catchPokémon("Jolteon");
console.log(firstAttempt.name);
// Output : "Jolteon"
firstAttempt.catchPokémon.call(secondAttempt, "Umbreon");
console.log(secondAttempt.name);
// Output : "Umbreon"
var thirdAttempt = new firstAttempt.catchPokémon("Eevee");
console.log(firstAttempt.name);
// Output : "Jolteon"
console.log(thirdAttempt.name);
// Output : "Eevee"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment