Skip to content

Instantly share code, notes, and snippets.

@ms314006
Last active April 12, 2021 16:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms314006/73c506a8589f63cb948d1483654fb0c6 to your computer and use it in GitHub Desktop.
Save ms314006/73c506a8589f63cb948d1483654fb0c6 to your computer and use it in GitHub Desktop.
const copyObj = (originObj) => {
const originPrototype = Object.getPrototypeOf(originObj);
let newObj = Object.create(originPrototype);
const originObjOwnProperties = Object.getOwnPropertyNames(originObj);
originObjOwnProperties.forEach((property) => {
const prototypeDesc = Object.getOwnPropertyDescriptor(originObj, property);
Object.defineProperty(newObj, property, prototypeDesc);
});
return newObj;
}
class Printer {
print(text, style = '') {
console.log(`%c${text}`, style)
}
}
const yellowStyle = (printer) => {
const decorator = copyObj(printer);
decorator.print = (text, style = '') => {
printer.print(text, `${style}color: yellow;`);
};
return decorator;
};
const boldStyle = (printer) => {
const decorator = copyObj(printer);
decorator.print = (text, style = '') => {
printer.print(text, `${style}font-weight:bold;`);
};
return decorator;
};
const bigSizeStyle = (printer) => {
const decorator = copyObj(printer);
decorator.print = (text, style = '') => {
printer.print(text, `${style}font-size: 36px;`);
};
return decorator;
};
const printer = boldStyle(bigSizeStyle(yellowStyle(new Printer())));
printer.print('something');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment