This file contains 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
const IronMan = stampit(Flying, Intelligence); | |
const Hulk = stampit(SuperStrength, Intelligence); | |
const Thor = stampit(SuperStrength, Flying); |
This file contains 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
const tonyStark = IronMan({ name: 'Tony Stark' }); | |
tonyStark.fly(); | |
const bruceBanner = Hulk({ name: 'Bruce Banner', stamina: 10000 }); | |
bruceBanner.throwBoulder(); |
This file contains 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
const mailgun = require('mailgun-js')({ | |
apiKey: api_key, | |
domain: domain | |
}); | |
const EmailStrategy = { | |
send: function(receiptHtml, userEmail) { | |
const emailBody = getEmailBody(receiptHtml, userEmail); | |
mailgun.messages().send(emailBody, function (error, body) { | |
console.log(body); |
This file contains 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
const slack = require('slack') | |
const SlackStrategy = { | |
send: function(receiptHtml, userEmail) { | |
const user = getUserFromEmail(userEmail); | |
const slackMessageBody = getSlackMessageBody(receiptHtml, userEmail); | |
slack.chat.postEphemeral({ token, channel, slackMessageBody, user }) | |
} | |
} |
This file contains 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 ReceiptManager { | |
_generateReceiptHtml(orderId, userEmail) { | |
// generates and returns HTML of the receipt | |
} | |
sendReceipt(orderId, userEmail, sendStrategy) { | |
const receiptHTML = this._generateReceiptHtml(orderId, userEmail); | |
sendStrategy.send(receiptHTML, userEmail); |
This file contains 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
sendReceipt(orderId, userEmail, client) { | |
const receiptHTML = this._generateReceiptHtml(orderId, userEmail); | |
switch(client) { | |
case 'email': { | |
sendEmail(receiptHTML, userEmail); | |
break; | |
} | |
case 'slack': { | |
sendSlackMessage(receiptHTML, userEmail); | |
break; |
This file contains 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
const ReceiptManager = require('./receipt-manager'); | |
const { | |
EmailStrategy, | |
SlackStrategy, | |
} = require('./strategy'); | |
function main(orderId, userEmail) { | |
const manager = new ReceiptManager(); | |
const email = new EmailStrategy(); |
This file contains 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
function Hedgehog () { | |
let speed = 10000; // this is private | |
this.name = 'Sonic'; | |
this.zoom = function () { | |
// both name and speed are accessible from here | |
console.log(`${this.name} zooms with the speed of ${speed} miles per second!`); | |
} | |
} | |
This file contains 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 Hedgehog { | |
constructor() { | |
let speed = 10000; //this is private | |
this.name = 'Sonic'; | |
this.zoom = function () { | |
// can access both name and speed from here | |
console.log(`${this.name} zooms with the speed of ${speed} miles per second!`); | |
} | |
} | |
This file contains 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
let Hedgehog = (function () { | |
let privateProps = new WeakMap(); | |
class Hedgehog { | |
constructor(name) { | |
this.name = name; // this is public | |
privateProps.set(this, { speed: 1000 }); // this is private | |
} | |
zoom() { |