亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

深入理解JavaScript函數(shù)參數(shù)(推薦)

 更新時間:2016年07月26日 11:27:19   作者:小火柴的藍色理想  
這篇文章主要介紹了深入理解JavaScript函數(shù)參數(shù)(推薦)的相關資料,需要的朋友可以參考下

前面的話

  javascript函數(shù)的參數(shù)與大多數(shù)其他語言的函數(shù)的參數(shù)有所不同。函數(shù)不介意傳遞進來多少個參數(shù),也不在乎傳進來的參數(shù)是什么數(shù)據(jù)類型,甚至可以不傳參數(shù)。

arguments

  javascript中的函數(shù)定義并未指定函數(shù)形參的類型,函數(shù)調用也未對傳入的實參值做任何類型檢查。實際上,javascript函數(shù)調用甚至不檢查傳入形參的個數(shù)

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

同名形參

  在非嚴格模式下,函數(shù)中可以出現(xiàn)同名形參,且只能訪問最后出現(xiàn)的該名稱的形參

function add(x,x,x){
return x;
}
console.log(add(1,2,3));//3

  而在嚴格模式下,出現(xiàn)同名形參會拋出語法錯誤

function add(x,x,x){
'use strict';
return x;
}
console.log(add(1,2,3));//SyntaxError: Duplicate parameter name not allowed in this context

參數(shù)個數(shù)

  當實參比函數(shù)聲明指定的形參個數(shù)要少,剩下的形參都將設置為undefined值

function add(x,y){
console.log(x,y);//1 undefined
}
add(1);

  常常使用邏輯或運算符給省略的參數(shù)設置一個合理的默認值

function add(x,y){
y = y || 2;
console.log(x,y);//1 2
}
add(1);

  [注意]實際上,使用y || 2是不嚴謹?shù)?,顯式地設置假值(undefined、null、false、0、-0、''、NaN)也會得到相同的結果。所以應該根據(jù)實際場景進行合理設置

  當實參比形參個數(shù)要多時,剩下的實參沒有辦法直接獲得,需要使用即將提到的arguments對象

  javascript中的參數(shù)在內部是用一個數(shù)組來表示的。函數(shù)接收到的始終都是這個數(shù)組,而不關心數(shù)組中包含哪些參數(shù)。在函數(shù)體內可以通過arguments對象來訪問這個參數(shù)數(shù)組,從而獲取傳遞給函數(shù)的每一個參數(shù)。arguments對象并不是Array的實例,它是一個類數(shù)組對象,可以使用方括號語法訪問它的每一個元素

function add(x){
console.log(arguments[0],arguments[1],arguments[2])//1 2 3
return x+1;
}
add(1,2,3);

  arguments對象的length屬性顯示實參的個數(shù),函數(shù)的length屬性顯示形參的個數(shù)

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

對象參數(shù)

  當一個函數(shù)包含超過3個形參時,要記住調用函數(shù)中實參的正確順序實在讓人頭疼

function arraycopy(/*array*/from,/*index*/form_start,/*array*/to,/*index*/to_start,/*integer*/length){
//todo
}

  通過名/值對的形式來傳入?yún)?shù),這樣參數(shù)的順序就無關緊要了。定義函數(shù)的時候,傳入的實參都寫入一個單獨的對象之中,在調用的時候傳入一個對象,對象中的名/值對是真正需要的實參數(shù)據(jù)

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});

同步

  當形參與實參的個數(shù)相同時,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);

  [注意]雖然命名參數(shù)和對應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對象的函數(shù)

  下面是經(jīng)典的階乘函數(shù)

function factorial(num){
if(num <=1){
return 1;
}else{
return num* factorial(num-1);
}
} 
console.log(factorial(5));//120

  但是,上面這個函數(shù)的執(zhí)行與函數(shù)名緊緊耦合在了一起,可以使用arguments.callee可以消除函數(shù)解耦

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));

  這時,可以使用具名的函數(shù)表達式

var factorial = function fn(num){
if(num <=1){
return 1;
}else{
return num*fn(num-1);
}
}; 
console.log(factorial(5));//120

【caller】

  實際上有兩個caller屬性

【1】函數(shù)的caller

  函數(shù)的caller屬性保存著調用當前函數(shù)的函數(shù)的引用,如果是在全局作用域中調用當前函數(shù),它的值是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和函數(shù)的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);

函數(shù)重載

  javascript函數(shù)不能像傳統(tǒng)意義上那樣實現(xiàn)重載。而在其他語言中,可以為一個函數(shù)編寫兩個定義,只要這兩個定義的簽名(接受的參數(shù)的類型和數(shù)量)不同即可

  javascript函數(shù)沒有簽名,因為其參數(shù)是由包含0或多個值的數(shù)組來表示的。而沒有函數(shù)簽名,真正的重載是不可能做到的

//后面的聲明覆蓋了前面的聲明
function addSomeNumber(num){
return num + 100;
}
function addSomeNumber(num){
return num + 200;
}
var result = addSomeNumber(100);//300

  只能通過檢查傳入函數(shù)中參數(shù)的類型和數(shù)量并作出不同的反應,來模仿方法的重載

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

參數(shù)傳遞

  javascript中所有函數(shù)的參數(shù)都是按值傳遞的。也就是說,把函數(shù)外部的值復制到函數(shù)內部的參數(shù),就和把值從一個變量復制到另一個變量一樣

【1】基本類型值

  在向參數(shù)傳遞基本類型的值時,被傳遞的值會被復制給一個局部變量(命名參數(shù)或arguments對象的一個元素)

function addTen(num){
num += 10;
return num;
}
var count = 20;
var result = addTen(count);
console.log(count);//20,沒有變化
console.log(result);//30

【2】引用類型值

  在向參數(shù)傳遞引用類型的值時,會把這個值在內存中的地址復制給一個局部變量,因此這個局部變量的變化會反映在函數(shù)的外部

function setName(obj){
obj.name = 'test';
}
var person = new Object();
setName(person);
console.log(person.name);//'test'

  當在函數(shù)內部重寫引用類型的形參時,這個變量引用的就是一個局部對象了。而這個局部對象會在函數(shù)執(zhí)行完畢后立即被銷毀

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函數(shù)參數(shù)(推薦),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

最新評論