1. 程式人生 > >鏈式呼叫 小例項(附推導過程,關聯到 物件、 鏈式呼叫 、 函式執行 、 函式返回、形參實參、undefined)

鏈式呼叫 小例項(附推導過程,關聯到 物件、 鏈式呼叫 、 函式執行 、 函式返回、形參實參、undefined)

    var foo=function(m,n){
            console.log(n);
            return{
                foo:function(o){
                    console.log(o);          
                    return foo(o,m);
                }
            }
        }


     var result=foo(2).foo(3).foo(4).foo(5).foo().foo(6);
    
// foo(2) m=2,n=undefined, - undefined { foo:fn() } // 3 o=3 - 3 foo(3,2) m=3,n=2 - 2 {foo} // 4 o=4 - 4 foo(4,3) m=4 n=3 - 3 {foo} // 5 o=5 - 5 foo(5,4) m=5 n=4 - 4 { foo }
// () o= undefined - undefined foo(undefined,5) m=undefined n=5 - 5 { foo } // 6 o= 6 - 6 f00(6,undefined) undefined // undefined // 3 // 2 // 4 // 3 // 5 // 4 // undefined //
5 // 6 // undefined