js 函数参数
函數有了參數才變得強大。
一、參數規則
形參比實參多,多的形參賦值undefined
實參比形參多,有多少形參就對應賦多少實參,其余的放到 arguments
arguments[] 是一個類數組 – 實參列表
二、參數的長度
function fun(a) {document.write(fun.length); // 1 --- 形參的長度document.write(argumens.length); // 3 --- 實參的長度 }fun(1, 2, 3);小案例 – 輸入數組多少就加多少
function sum() {let result = 0;for (let i = 0; i < arguments.length; i++) {result += arguments[i];}document.write(result); }sum(1, 2, 3, 4, 5, 6, 7, 8, 9); // 45小案例 – 判斷形參多了還是實參多了
function test(a, b, c, d) {if (test.length > arguments.length) {console.log('形參多了');} else if (test.length < arguments.length) {console.log('實參多了');} else {console.log('相等');} }test(1, 2, 3, 4); // 相等 test(1, 2); // 形參多了映射規則:你變我也變,我變你也變
只有形參和實參數量相等的情況下,他們才有映射規則;如果數量不相等,沒有映射關系!!!
總結
- 上一篇: linux ortp 编译,Linux下
- 下一篇: python 一次性定时器_python