Skip to content

Instantly share code, notes, and snippets.

@alvarezmauro
Last active May 8, 2018 00:36
Show Gist options
  • Save alvarezmauro/959b132b931fe08b71ee043b6f4ee350 to your computer and use it in GitHub Desktop.
Save alvarezmauro/959b132b931fe08b71ee043b6f4ee350 to your computer and use it in GitHub Desktop.
JS design pattern - Chaning
var objWithChaining = function objWithChaining(someValue){
var self = this; // Capture "this" object because the caller may not be trusted ಠ_ಠ
this.methodOne = function methodOne(methodParam){
// method implementation
return self;
}
this.methodTwo = function methodTwo(methodParam){
// method implementation
return self;
}
this.methodThree = function methodThree(methodParam){
// method implementation
return self;
}
this.callbackMethod = function callbackMethod(callback){
callback(someValue);
return self;
}
}
new objWithChaining(0)
.methodOne(10)
.methodTwo(100)
.methodThree(1000)
.callbackMethod(function(someValue){
console.log(someValue)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment