Skip to content

Instantly share code, notes, and snippets.

@AnimeshPandey
AnimeshPandey / 1_this_Example_1.js
Last active February 27, 2021 23:52
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()
}
@AnimeshPandey
AnimeshPandey / 1_this_Example_2.js
Last active February 27, 2021 23:52
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 2. Default Binding.
function getPokémon(){
console.log("Pokémon in Ultra Ball is : ", this.ultraBall);
}
var ultraBall = "Articuno";
getPokémon();
// Output
// Pokémon in Ultra Ball is : Articuno
@AnimeshPandey
AnimeshPandey / 1_this_Example_3.js
Last active February 27, 2021 23:50
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 3. Simple Implicit Binding
function getBaseSpeed(){
console.log("Base Speed Stat is : ", this.baseSpeed);
}
var pikachu = {
baseSpeed : 90,
getBaseSpeed : getBaseSpeed
};
@AnimeshPandey
AnimeshPandey / 1_this_Example_4.js
Last active February 27, 2021 23:51
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 4. Last level of an Object property chain
// matters for Implicit 'this' binding
function getBaseSpeed(){
console.log("Base Speed Stat is : ", this.baseSpeed);
}
var pikachu = {
baseSpeed : 90,
getBaseSpeed : getBaseSpeed
};
@AnimeshPandey
AnimeshPandey / 1_this_Example_5.js
Created February 27, 2021 23:50
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 5. Lost implicit "this" binding #1
function getBaseSpeed(){
console.log("Base Speed Stat is : ", this.baseSpeed);
}
var pikachu = {
baseSpeed : 90,
getBaseSpeed : getBaseSpeed
};
@AnimeshPandey
AnimeshPandey / 1_this_Example_6.js
Created February 27, 2021 23:54
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 6. Lost implicit "this" binding #2
function getBaseSpeed(){
console.log("Base Speed Stat is : ", this.baseSpeed);
}
function executeFunction(fn) {
fn();
}
var pikachu = {
@AnimeshPandey
AnimeshPandey / 1_this_Example_7.js
Created February 27, 2021 23:55
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 7. 'this' binding with bind()
var bulbasaur = {
pokédexNo: "001",
getPokédexNo: function() {
return this.pokédexNo;
}
};
var unboundGetPokédexNo = bulbasaur.getPokédexNo;
console.log(unboundGetPokédexNo());
@AnimeshPandey
AnimeshPandey / 1_this_Example_8.js
Created February 27, 2021 23:58
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()
@AnimeshPandey
AnimeshPandey / 1_this_Example_9.js
Created February 27, 2021 23:59
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 9. Simple 'this' binding in case of new
function setPokémon(name) {
this.name = name;
}
var catchPokémon = new setPokémon("Blastoise");
console.log(catchPokémon.name);
// Output
// "Blastoise"
@AnimeshPandey
AnimeshPandey / 1_this_Example_10.js
Created February 28, 2021 00:01
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 10. Implicit versus Explicit
function catchPokémon() {
console.log("Pokémon caught : ", this.name);
}
var firstAttempt = {
name : "Flareon",
catchPokémon : catchPokémon
}