Skip to content

Instantly share code, notes, and snippets.

@snippe
Created November 9, 2014 01:27
Show Gist options
  • Save snippe/c0fe47023dc01ba78239 to your computer and use it in GitHub Desktop.
Save snippe/c0fe47023dc01ba78239 to your computer and use it in GitHub Desktop.
.bind in JavaScript
this.statusId = 1;
var module = {
statusId : 4,
getStatus : function(){
return this.statusId;
}
};
console.log(module.getStatus()); //4
var getModuleStatus = module.getStatus;
console.log(getModuleStatus()); //this refers to global //1
var boundGetStatus = getModuleStatus.bind(module);
console.log(boundGetStatus()); //4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment