Skip to content

Instantly share code, notes, and snippets.

@AnimeshPandey
Created February 27, 2021 23:58
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/def3e56168a4d26797153e5b1c0ba62b to your computer and use it in GitHub Desktop.
Save AnimeshPandey/def3e56168a4d26797153e5b1c0ba62b 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 8. 'this' binding with call() or apply()
function Pokémon(name, type) {
this.name = name;
this.type = type;
}
function PokémonExtension(name, type, species) {
// with call()
Pokémon.call(this, name, type);
// with apply()
// Pokémon.apply(this, [name, type]);
this.species = species;
}
var quilava = new PokémonExtension("Quilava", "Fire", "Volcano Pokémon");
console.log(quilava.name, quilava.type, quilava.species);
// Output
// "Quilava" "Fire" "Volcano Pokémon"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment