1. 程式人生 > >javascript函數自整理

javascript函數自整理

span nts st3 code 傳參 優先 每次 console scrip

1.定義函數的3種方式:

//1.function語句只會解釋一次,並且後面的會覆蓋前面的,優先解析
function test1(){
    console.log(‘test1‘);
}
test1();

//2.函數直接量只會解釋一次,並且後面的會覆蓋前面的,順序解析
var test2=function(){
    console.log(‘test2‘);
}
test2();

//3.每次執行每次動態new一次
var test3=new Function("a","b","return a+b;");//頂級作用域,順序解析
console.log(test3(1,2));

2.函數的參數:

function fun(a,b,c,d){
    console.log(fun.length);//形參個數

    //arguments 對象:可以訪問實際參數個數,內部是一個數組,只能在函數內部使用,常用於遞歸操作
    console.log(arguments.length);
    console.log(typeof arguments);
    console.log(arguments);

    //arguments.callee指向函數自身(fun)
    if( arguments.callee.length == arguments.length ){
        
return a+b; }else{ console.log(‘傳參錯誤!‘); } } fun(1,2,3);

javascript函數自整理