Skip to content

Instantly share code, notes, and snippets.

@npras
Last active February 22, 2017 12:54
Show Gist options
  • Save npras/23d6ade876344174225b1b5f8abc39a7 to your computer and use it in GitHub Desktop.
Save npras/23d6ade876344174225b1b5f8abc39a7 to your computer and use it in GitHub Desktop.
Gist explaining method binding to outer scope
let myLib = (function(){
this.libName = 'in lib';
let obj = {
libName: 'in obj',
toCaps() {
return this.libName.toUpperCase();
}
};
console.log(obj.toCaps()); // IN OBJ (bound to the object)
boundToCaps = obj.toCaps.bind(this);
console.log(boundToCaps()); // IN LIB (bound to the myLib scope)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment