# Perf: Function ``` function store(value) { return value; } /* 1. 假設不使用 TypeScript 的情況下,第一和第二個參數預設要計算數字,第三個參數則是判斷是否存入全域 2. 透過 typeof 檢查傳入參數,是否有以下錯誤情境 2.1 型別是否正確 2.2 有無漏傳參數 2.3 寫錯參數傳入位置 3. 使用 arrow function 精簡程式碼 4. 使用 ES6 的 default parameter,讓第三個參數為 options,預設為 false */ const calculateAndStore = ((a, b, shouldStore = false) => { if (typeof a !== 'number' || typeof b !== 'number') { console.log(`Please check your input, a: ${a}, b: ${b}`); return; } if (typeof shouldStore !== 'boolean') { console.log(`Please check your input, shouldStore: ${shouldStore}`); return; } const value = a + b; if (shouldStore) { store(value); } return value; }); const newCalculateApp1 = calculateAndStore(10, 20, true); console.log(newCalculateApp1); const newCalculateApp2 = calculateAndStore(10, 20); console.log(newCalculateApp2); const newCalculateApp3 = calculateAndStore(10, true, 20); console.log(newCalculateApp3); const newCalculateApp4 = calculateAndStore(10, 20, 100); console.log(newCalculateApp4); ```
×
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