Skip to content

Instantly share code, notes, and snippets.

Avatar
💻

Animesh Pandey AnimeshPandey

💻
View GitHub Profile
@AnimeshPandey
AnimeshPandey / 1_this_Example_12.js
Created Feb 28, 2021
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
View 1_this_Example_12.js
// 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"
@AnimeshPandey
AnimeshPandey / 1_this_Example_11.js
Last active Feb 28, 2021
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
View 1_this_Example_11.js
// 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_10.js
Created Feb 28, 2021
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
View 1_this_Example_10.js
// Example 10. Implicit versus Explicit
function catchPokémon() {
console.log("Pokémon caught : ", this.name);
}
var firstAttempt = {
name : "Flareon",
catchPokémon : catchPokémon
}
@AnimeshPandey
AnimeshPandey / 1_this_Example_9.js
Created Feb 27, 2021
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
View 1_this_Example_9.js
// 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_8.js
Created Feb 27, 2021
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
View 1_this_Example_8.js
// 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_7.js
Created Feb 27, 2021
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
View 1_this_Example_7.js
// 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_6.js
Created Feb 27, 2021
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
View 1_this_Example_6.js
// 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_5.js
Created Feb 27, 2021
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
View 1_this_Example_5.js
// 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_4.js
Last active Feb 27, 2021
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
View 1_this_Example_4.js
// 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_3.js
Last active Feb 27, 2021
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
View 1_this_Example_3.js
// Example 3. Simple Implicit Binding
function getBaseSpeed(){
console.log("Base Speed Stat is : ", this.baseSpeed);
}
var pikachu = {
baseSpeed : 90,
getBaseSpeed : getBaseSpeed
};