Skip to content

Instantly share code, notes, and snippets.

@jdaly13
Created May 15, 2013 04:40
Show Gist options
  • Save jdaly13/5581679 to your computer and use it in GitHub Desktop.
Save jdaly13/5581679 to your computer and use it in GitHub Desktop.
how to chain methods using an object literal (always return this)
var myBooze = {
booze:['wine', 'beer', 'liquor'],
chosenBooze:[],
getBooze: function (type) {
var booze_length = this.booze.length
for (i=0; i < booze_length; i++) {
if (type === this.booze[i]) {
this.chosenBooze.push(this.booze[i]);
return this
} else {
return false;
}
}
},
consumption_style: function () {
var consumption;
var liquor = this.chosenBooze[0];
if (liquor === 'wine') {
return "i got class"
}
}
}
myBooze.getBooze('wine').consumption_style() // this will return "I got class"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment