Skip to content

Instantly share code, notes, and snippets.

@doug2k1
Created September 6, 2017 13:25
Show Gist options
  • Save doug2k1/473d3f33b6a78d05a0938449c78a0c8c to your computer and use it in GitHub Desktop.
Save doug2k1/473d3f33b6a78d05a0938449c78a0c8c to your computer and use it in GitHub Desktop.
const player = {
name: 'Cloud'
}
const enemy = {
name: 'Sephirot'
}
// Função declarada global.
// Se chamar direto, o 'this' seria 'global' ou 'window'.
const sayName = function () {
console.log(this.name)
}
// Usando 'bind' para criar novas funções que fazem a mesma coisa
// da função 'sayName', mas definindo o 'this' manualmente.
const sayPlayerName = sayName.bind(player)
const sayEnemyName = sayName.bind(enemy)
sayPlayerName() // Cloud
sayEnemyName() // Sephirot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment