Last active
April 12, 2021 16:06
-
-
Save ms314006/73c506a8589f63cb948d1483654fb0c6 to your computer and use it in GitHub Desktop.
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 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