Skip to content

Instantly share code, notes, and snippets.

@jameswomack
Last active August 29, 2015 13:59
Show Gist options
  • Save jameswomack/10618003 to your computer and use it in GitHub Desktop.
Save jameswomack/10618003 to your computer and use it in GitHub Desktop.
Examples of eliminating `self = this`
/* WRONG */
function Sobachi(){
this.prefecture = 'Iwate';
this.characterName = 'Sobatchi';
this.friends = [];
}
Sobachi.prototype.addFriends = function(friends){
var self = this;
friends.forEach(function(friend){
if(self.characterName != friend.characterName){
self.friends.push(friend);
}
});
};
/* RIGHT */
function Sobachi(){
this.prefecture = 'Iwate';
this.characterName = 'Sobatchi';
this.friends = [];
}
function addFriend(friend){
(this.characterName != friend.characterName) && (this.friends.push(friend));
};
Sobachi.prototype.addFriends = function(friends){
friends.forEach(addFriend.bind(this));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment