前面的話
javascript函數的參數與大多數其他語言的函數的參數有所不同。函數不介意傳遞進來多少個參數,也不在乎傳進來的參數是什麼數據類型,甚至可以不傳參數。
arguments
javascript中的函數定義並未指定函數形參的類型,函數調用也未對傳入的實參值做任何類型檢查。實際上,javascript函數調用甚至不檢查傳入形參的個數
function add(x){
return x+1;
}
console.log(add(1));//2
console.log(add('1'));//'11'
console.log(add());//NaN
console.log(add(1,2));//2
同名形參
在非嚴格模式下,函數中可以出現同名形參,且只能訪問最後出現的該名稱的形參
function add(x,x,x){
return x;
}
console.log(add(1,2,3));//3
而在嚴格模式下,出現同名形參會拋出語法錯誤
function add(x,x,x){
'use strict';
return x;
}
console.log(add(1,2,3));//SyntaxError: Duplicate parameter name not allowed in this context
參數個數
當實參比函數聲明指定的形參個數要少,剩下的形參都將設置為undefined值
function add(x,y){
console.log(x,y);//1 undefined
}
add(1);
常常使用邏輯或運算符給省略的參數設置一個合理的默認值
function add(x,y){
y = y || 2;
console.log(x,y);//1 2
}
add(1);
[注意]實際上,使用y || 2是不嚴謹的,顯式地設置假值(undefined、null、false、0、-0、''、NaN)也會得到相同的結果。所以應該根據實際場景進行合理設置
當實參比形參個數要多時,剩下的實參沒有辦法直接獲得,需要使用即將提到的arguments對象
javascript中的參數在內部是用一個數組來表示的。函數接收到的始終都是這個數組,而不關心數組中包含哪些參數。在函數體內可以通過arguments對象來訪問這個參數數組,從而獲取傳遞給函數的每一個參數。arguments對象並不是Array的實例,它是一個類數組對象,可以使用方括號語法訪問它的每一個元素
function add(x){
console.log(arguments[0],arguments[1],arguments[2])//1 2 3
return x+1;
}
add(1,2,3);
arguments對象的length屬性顯示實參的個數,函數的length屬性顯示形參的個數
function add(x,y){
console.log(arguments.length)//3
return x+1;
}
add(1,2,3);
console.log(add.length);//2
形參只是提供便利,但不是必需的
function add(){
return arguments[0] + arguments[1];
}
console.log(add(1,2));//3
對象參數
當一個函數包含超過3個形參時,要記住調用函數中實參的正確順序實在讓人頭疼
function arraycopy(/*array*/from,/*index*/form_start,/*array*/to,/*index*/to_start,/*integer*/length){
//todo
}
通過名/值對的形式來傳入參數,這樣參數的順序就無關緊要了。定義函數的時候,傳入的實參都寫入一個單獨的對象之中,在調用的時候傳入一個對象,對象中的名/值對是真正需要的實參數據
function easycopy(args){
arraycopy(args.from,args.form_start || 0,args.to,args.to_start || 0, args.length);
}
var a = [1,2,3,4],b =[];
easycopy({form:a,to:b,length:4});
同步
當形參與實參的個數相同時,arguments對象的值和對應形參的值保持同步
function test(num1,num2){
console.log(num1,arguments[0]);//1 1
arguments[0] = 2;
console.log(num1,arguments[0]);//2 2
num1 = 10;
console.log(num1,arguments[0]);//10 10
}
test(1);
[注意]雖然命名參數和對應arguments對象的值相同,但並不是相同的命名空間。它們的命名空間是獨立的,但值是同步的
但在嚴格模式下,arguments對象的值和形參的值是獨立的
function test(num1,num2){
'use strict';
console.log(num1,arguments[0]);//1 1
arguments[0] = 2;
console.log(num1,arguments[0]);//1 2
num1 = 10;
console.log(num1,arguments[0]);//10 2
}
test(1);
當形參並沒有對應的實參時,arguments對象的值與形參的值並不對應
function test(num1,num2){
console.log(num1,arguments[0]);//undefined,undefined
num1 = 10;
arguments[0] = 5;
console.log(num1,arguments[0]);//10,5
}
test();
內部屬性
【callee】
arguments對象有一個名為callee的屬性,該屬性是一個指針,指向擁有這個arguments對象的函數
下面是經典的階乘函數
function factorial(num){
if(num <=1){
return 1;
}else{
return num* factorial(num-1);
}
}
console.log(factorial(5));//120
但是,上面這個函數的執行與函數名緊緊耦合在了一起,可以使用arguments.callee可以消除函數解耦
function factorial(num){
if(num <=1){
return 1;
}else{
return num* arguments.callee(num-1);
}
}
console.log(factorial(5));//120
但在嚴格模式下,訪問這個屬性會拋出TypeError錯誤
function factorial(num){
'use strict';
if(num <=1){
return 1;
}else{
return num* arguments.callee(num-1);
}
}
//TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
console.log(factorial(5));
這時,可以使用具名的函數表達式
var factorial = function fn(num){
if(num <=1){
return 1;
}else{
return num*fn(num-1);
}
};
console.log(factorial(5));//120
【caller】
實際上有兩個caller屬性
【1】函數的caller
函數的caller屬性保存著調用當前函數的函數的引用,如果是在全局作用域中調用當前函數,它的值是null
function outer(){
inner();
}
function inner(){
console.log(inner.caller);//outer(){inner();}
}
outer();
function inner(){
console.log(inner.caller);//null
}
inner();
在嚴格模式下,訪問這個屬性會拋出TypeError錯誤
function inner(){
'use strict';
//TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context
console.log(inner.caller);
}
inner();
【2】arguments對象的caller
該屬性始終是undefined,定義這個屬性是為了分清arguments.caller和函數的caller屬性
function inner(x){
console.log(arguments.caller);//undefined
}
inner(1);
同樣地,在嚴格模式下,訪問這個屬性會拋出TypeError錯誤
function inner(x){
'use strict';
//TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context
console.log(arguments.caller);
}
inner(1);
函數重載
javascript函數不能像傳統意義上那樣實現重載。而在其他語言中,可以為一個函數編寫兩個定義,只要這兩個定義的簽名(接受的參數的類型和數量)不同即可
javascript函數沒有簽名,因為其參數是由包含0或多個值的數組來表示的。而沒有函數簽名,真正的重載是不可能做到的
//後面的聲明覆蓋了前面的聲明
function addSomeNumber(num){
return num + 100;
}
function addSomeNumber(num){
return num + 200;
}
var result = addSomeNumber(100);//300
只能通過檢查傳入函數中參數的類型和數量並作出不同的反應,來模仿方法的重載
function doAdd(){
if(arguments.length == 1){
alert(arguments[0] + 10);
}else if(arguments.length == 2){
alert(arguments[0] + arguments[1]);
}
}
doAdd(10);//20
doAdd(30,20);//50
參數傳遞
javascript中所有函數的參數都是按值傳遞的。也就是說,把函數外部的值復制到函數內部的參數,就和把值從一個變量復制到另一個變量一樣
【1】基本類型值
在向參數傳遞基本類型的值時,被傳遞的值會被復制給一個局部變量(命名參數或arguments對象的一個元素)
function addTen(num){
num += 10;
return num;
}
var count = 20;
var result = addTen(count);
console.log(count);//20,沒有變化
console.log(result);//30
【2】引用類型值
在向參數傳遞引用類型的值時,會把這個值在內存中的地址復制給一個局部變量,因此這個局部變量的變化會反映在函數的外部
function setName(obj){
obj.name = 'test';
}
var person = new Object();
setName(person);
console.log(person.name);//'test'
當在函數內部重寫引用類型的形參時,這個變量引用的就是一個局部對象了。而這個局部對象會在函數執行完畢後立即被銷毀
function setName(obj){
obj.name = 'test';
console.log(person.name);//'test'
obj = new Object();
obj.name = 'white';
console.log(person.name);//'test'
}
var person = new Object();
setName(person);
以上所述是小編給大家介紹的深入理解JavaScript函數參數(推薦),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!