--- robots: noindex, nofollow tags: refactoring --- # Preserve Whole Object > 練習用的 [REPL](https://repl.it/@yaosiang/PreserveWholeObject) ```javascript= const low = aRoom.daysTempRange.low; const high = aRoom.daysTempRange.high; if (!aPlan.withinRange(low, high)) alerts.push("room temperature went outside range"); class HeatingPlan { withinRange(bottom, top) { return (bottom >= this._temperatureRange.low) && (top <= this._temperatureRange.high); } } ``` 建立一個新 function,但名稱可以亂取。 ```javascript= class HeatingPlan { withinRange(bottom, top) { return (bottom >= this._temperatureRange.low) && (top <= this._temperatureRange.high); } // 建立一個新 function,但運算邏輯委派給原有 function xxNEWwithinRange(aNumberRange) { return this.withinRange(aNumberRange.low, aNumberRange.high); } } ``` ```javascript= const low = aRoom.daysTempRange.low; const high = aRoom.daysTempRange.high; if (!aPlan.xxNEWwithinRange(aRoom.daysTempRange)) alerts.push("room temperature went outside range"); class HeatingPlan { // withinRange(bottom, top) { // return (bottom >= this._temperatureRange.low) && (top <= this._temperatureRange.high); // } // 把運算邏輯搬回新的 function xxNEWwithinRange(aNumberRange) { return (aNumberRange.low >= this._temperatureRange.low) && (aNumberRange.high <= this._temperatureRange.high); } } ``` 把新 function 名稱改成原本的名字。 ```javascript= if (!aPlan.withinRange(aRoom.daysTempRange)) alerts.push("room temperature went outside range"); class HeatingPlan { withinRange(aNumberRange) { return (aNumberRange.low >= this._temperatureRange.low) && (aNumberRange.high <= this._temperatureRange.high); } } ```