當函式有很多個參數時,要調用此函式時,會很難記住要傳入引數的正確順序。因此讓引數可以作為任意順序的**『名稱/值』對組 (name/value pairs)** 傳入,可讓使用者使用此函式時,不需要記住引數的順序 。
定義函式並使它預期傳入單一物件引數,然後調用函式時,傳入一個所需『名稱/值』對組的物件:
// 書中範例
// 1. 從陣列 from 複製 length 個元素到陣列 to
// 2. 從 from 陣列 的索引 from_start 開始複製
// 3. 把該元素複製到 to 陣列的 to_start 索引位置。
/* 要記住引數的順序並不簡單 
 型別 array : from, 
 型別 index : from_start, 
 型別 array : to, 
 型別 index : to_start, 
 型別 integer : length
*/
function arraycopy(from, from_start, to, to_start, length){
	//程式碼
}
// 這個版本的效能低一點,但不需要記得引數的順序,而 from_start 和 to_start 的預設值為0。
// easycopy 函式接受一個物件作為引數
// 物件包含 from、from_start、to、to_start 和 length 屬性
// from_start 和 to_start 的預設值為0
function easycopy(args){
// 使用 args.from、args.from_start、args.to 和 args.to_start 來存取傳入函式的引數值
// 若某個引數沒有被傳入,則預設值為0
	arraycopy(args.from, 
			  args.form_start || 0, 
			  args.to, 
			  args.to_start || 0, 
			  args.length);
}
// 調用 easycopy() 的方式
var a = [1,2,3,4], b = [];
//從陣列 a 複製4個元素到陣列 b 中
easycopy({from: a, to: b, length: 4});
function myFunction(options) {
  var x = options.x || 0;
  var y = options.y || 0;
  var z = options.z || 0;
  console.log(x, y, z);
}
myFunction({y: 2, x: 1, z: "banana"}); //  "1 2 banana"
...)註解。/**
 * This function takes in three parameters: x, y, and z.
 * @param {number} x - The value of x.
 * @param {number} y - The value of y.
 * @param {string} [z] - The value of z (optional).
 */
function myFunction(x, y, z) {
  console.log(x, y, z);
}
myFunction(1, 2); //  "1 2 undefined"
myFunction(1, 2, "banana"); //  "1 2 banana"
// 3. 可為函式引數使用具描述性的名稱,或在註解中加上引數的型別。
function fruitBasket(fruitName /* string */, quantity /* number */) {
  console.log(`You have ${quantity} ${fruitName}(s) in your basket.`);
}
// 4. 非必須的引數,可在註解中加上"optional"
function calculateTotal(price, taxRate /* optional */) {
  if (taxRate === undefined) {
    taxRate = 0.1; // default tax rate
  }
  return price * (1 + taxRate);
}
// 5. 若一個方法能夠接受任意數目的引數,則可用省略符號(ellipsis; `...`)註解。
function fruitSalad(...fruits) {
  console.log(`You have a fruit salad with ${fruits.join(', ')}.`);
}
fruitSalad("apple", "banana", "citrus", "date", "Elderberry", "fig ", "grape", "honeydew");
// 印出 "You have a fruit salad with apple, banana, citrus, date, Elderberry, fig , grape, honeydew."