Skip to content

Instantly share code, notes, and snippets.

@AnimeshPandey
Last active February 27, 2021 23:52
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/3f49ddaa659fb049cd9979682ae74530 to your computer and use it in GitHub Desktop.
Save AnimeshPandey/3f49ddaa659fb049cd9979682ae74530 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 1. Call-sites and Call-stacks
function thunderbolt(){
debugger;
console.log("Using Thunderbolt!");
}
function attack(){
console.log("Attacking!");
thunderbolt(); // <- Call-site for thunderbolt()
}
function choosePikachu(){
console.log("Pikachu, I choose you!");
attack(); // <- Call-site for attack()
}
choosePikachu(); // <- Call-site for choosePikachu()
// Execution starts when choosePikachu() is called.
// Call stack : 0) choosePikachu
// choosePikachu() calls attack()
// Call stack : 0) choosePikachu, 1) attack
// attack() calls thunderbolt()
// Call stack : 0) choosePikachu, 1) attack, 2) thunderbolt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment