Last active
August 14, 2018 09:06
-
-
Save jyotendra/9fc5db51f4a89ea83442d0c1419fb947 to your computer and use it in GitHub Desktop.
Method chaining in classes in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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