--- robots: noindex, nofollow tags: refactoring --- # Replace Parameter with Query > 練習用的 [REPL](https://repl.it/@yaosiang/ReplaceParameterwithQuery) ```javascript= class Order { get finalPrice() { const basePrice = this.quantity * this.itemPrice; let discountLevel; if (this.quantity > 100) discountLevel = 2; else discountLevel = 1; return this.discountedPrice(basePrice, discountLevel); } discountedPrice(basePrice, discountLevel) { switch (discountLevel) { case 1: return basePrice * 0.95; case 2: return basePrice * 0.9; } } } ``` 先用 `Replace Temp with Query` 處理 discountLevel。 ```javascript= class Order { get finalPrice() { const basePrice = this.quantity * this.itemPrice; return this.discountedPrice(basePrice, this.discountLevel); } get discountLevel() { return (this.quantity > 100) ? 2 : 1; } discountedPrice(basePrice, discountLevel) { switch (this.discountLevel) { // 直接 query 即可 case 1: return basePrice * 0.95; case 2: return basePrice * 0.9; } } } ``` 由於 discountLevel 可以直接查詢,就不用當參數來傳遞了。 ```javascript= class Order { get finalPrice() { const basePrice = this.quantity * this.itemPrice; return this.discountedPrice(basePrice); // 刪除參數 } get discountLevel() { return (this.quantity > 100) ? 2 : 1; } discountedPrice(basePrice) { // 刪除 discountLevel 參數 switch (this.discountLevel) { case 1: return basePrice * 0.95; case 2: return basePrice * 0.9; } } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up