Skip to content

Instantly share code, notes, and snippets.

@jyotendra
Last active August 14, 2018 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyotendra/9fc5db51f4a89ea83442d0c1419fb947 to your computer and use it in GitHub Desktop.
Save jyotendra/9fc5db51f4a89ea83442d0c1419fb947 to your computer and use it in GitHub Desktop.
Method chaining in classes in javascript
class BotResponseFormatter {
formattedResponse;
constructor(botInterpretation) {
this.formattedResponse = botInterpretation;
}
/**
*
* @param {string} replyType possible options are 'text', 'option'
*/
addReplyType(replyType) {
this.formattedResponse.reply_type = replyType;
return this;
}
/**
*
* @param {string} reply
*/
addReply(reply) {
this.formattedResponse.reply = reply;
return this;
}
/**
*
* @param {Object[]} options
*/
addOptions(options) {
this.formattedResponse.options = options;
return this;
}
/**
* returns final response
*/
getResponse() {
return this.formattedResponse;
}
}
const a = new BotResponseFormatter({a: "hello"}).addReplyType("text").getResponse();
console.log(a); // {a: "hello", reply_type: "text"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment