12.04 - function decorators.js

来自「JS设计模式源代码」· JavaScript 代码 · 共 23 行

JS
23
字号
function upperCaseDecorator(func) {  return function() {    return func.apply(this, arguments).toUpperCase();  }}function getDate() {  return (new Date()).toString();}getDateCaps = upperCaseDecorator(getDate);alert(getDate()); // Returns Wed Sep 26 2007 20:11:02 GMT-0700 (PDT)alert(getDateCaps()); // Returns WED SEP 26 2007 20:11:02 GMT-0700 (PDT)BellDecorator.prototype.ringBellLoudly =     upperCaseDecorator(BellDecorator.prototype.ringBell);var myBicycle = new AcmeComfortCruiser(); myBicycle = new BellDecorator(myBicycle);alert(myBicycle.ringBell()); // Returns 'Bell rung.'alert(myBicycle.ringBellLoudly()); // Returns 'BELL RUNG.'

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?