函式參數如果未定義,就是 undefined
用 || OR 運算子來判斷
const link = (point, url) => {
let point = point || 10 //point是undefined的話就傳入10
let url = url || 'http://google.com' //url是undefined的話就傳入http://google.com
}
用 typeOf 來判斷
const link = (point, url) => {
let point = typeOf point !== 'undefined' ? point : 10 //point是undefined的話就傳入10
let url = typeOf url !== 'undefined' ? url : 'http://google.com' //url是undefined的話就傳入http://google.com
}
只有在傳入參數為 undefined 的情況下才會使用預設值
const link = (point = 10 , url = 'http://google.com') => {
...
}