This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example 1. Call-sites and Call-stacks | |
function thunderbolt(){ | |
debugger; | |
console.log("Using Thunderbolt!"); | |
} | |
function attack(){ | |
console.log("Attacking!"); | |
thunderbolt(); // <- Call-site for thunderbolt() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example 3. Simple Implicit Binding | |
function getBaseSpeed(){ | |
console.log("Base Speed Stat is : ", this.baseSpeed); | |
} | |
var pikachu = { | |
baseSpeed : 90, | |
getBaseSpeed : getBaseSpeed | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example 5. Lost implicit "this" binding #1 | |
function getBaseSpeed(){ | |
console.log("Base Speed Stat is : ", this.baseSpeed); | |
} | |
var pikachu = { | |
baseSpeed : 90, | |
getBaseSpeed : getBaseSpeed | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example 6. Lost implicit "this" binding #2 | |
function getBaseSpeed(){ | |
console.log("Base Speed Stat is : ", this.baseSpeed); | |
} | |
function executeFunction(fn) { | |
fn(); | |
} | |
var pikachu = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example 10. Implicit versus Explicit | |
function catchPokémon() { | |
console.log("Pokémon caught : ", this.name); | |
} | |
var firstAttempt = { | |
name : "Flareon", | |
catchPokémon : catchPokémon | |
} |
OlderNewer