Skip to content

Instantly share code, notes, and snippets.

const IronMan = stampit(Flying, Intelligence);
const Hulk = stampit(SuperStrength, Intelligence);
const Thor = stampit(SuperStrength, Flying);
const tonyStark = IronMan({ name: 'Tony Stark' });
tonyStark.fly();
const bruceBanner = Hulk({ name: 'Bruce Banner', stamina: 10000 });
bruceBanner.throwBoulder();
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);
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 })
}
}
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);
sendReceipt(orderId, userEmail, client) {
const receiptHTML = this._generateReceiptHtml(orderId, userEmail);
switch(client) {
case 'email': {
sendEmail(receiptHTML, userEmail);
break;
}
case 'slack': {
sendSlackMessage(receiptHTML, userEmail);
break;
const ReceiptManager = require('./receipt-manager');
const {
EmailStrategy,
SlackStrategy,
} = require('./strategy');
function main(orderId, userEmail) {
const manager = new ReceiptManager();
const email = new EmailStrategy();
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!`);
}
}
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!`);
}
}
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() {