javascript中return function與return function()的區別

參考https://stackoverflow.com/questions/7629891/functions-that-return-a-function-javascriptjavascript

 

 

問題:惟一的區別是return中的函數是否帶括號java

輸入:函數

function a() {

    alert('A!');

    function b(){
        alert('B!'); 
    }

    return b();
}

var s = a();
alert('break');
s();

 

輸出:ui

A!
B!
break

 

 

輸入:this

function a() {

    alert('A!');

    function b(){
        alert('B!'); 
    }

    return b;
}

var s = a();
alert('break');
s();

 

輸出:spa

A!
break
B!

 

回答1:.net

 

Assigning a variable to a function (without the parenthesis) copies the reference to the function. Putting the parenthesis at the end of a function name, calls the function, returning the functions return value.code

 

 

http://jsfiddle.net/kaleb/Las6w/blog

function a() {
    alert('A');// A未在此做用域定義
}
//alerts 'A', returns undefined

function b() {
    alert('B');
    return a; //返回的是一個函數
}
//alerts 'B', returns function a

function c() {
    alert('C');
    return a();//返回函數執行結果
}
//alerts 'C', alerts 'A', returns undefined

alert("Function 'a' returns " + a());
alert("Function 'b' returns " + b());
alert("Function 'c' returns " + c());

回答2:ip

 

Returning the function name without () returns a reference to the function, which can be assigned as you've done with var s = a()s now contains a reference to the function b(), and calling s() is functionally equivalent to calling b().

// Return a reference to the function b().
// In your example, the reference is assigned to var s
return b;

 

Calling the function with () in a return statement executes the function, and returns whatever value was returned by the function. It is similar to calling var x = b();, but instead of assigning the return value of b() you are returning it from the calling function a(). If the function b() itself does not return a value, the call returns undefined after whatever other work is done by b().

    
Returning the function name without () returns a reference to the function, which can be assigned as you've done with var s = a(). s now contains a reference to the function b(), and calling s() is functionally equivalent to calling b().

// Return a reference to the function b().
// In your example, the reference is assigned to var s
return b;
Calling the function with () in a return statement executes the function, and returns whatever value was returned by the function. It is similar to calling var x = b();, but instead of assigning the return value of b() you are returning it from the calling function a(). If the function b() itself does not return a value, the call returns undefined after whatever other work is done by b().

// Execute function b() and return its value
return b();
// If b() has no return value, this is equivalent to calling b(), followed by
// return undefined;

 回答3:

 

return b(); calls the function b(), and returns its result.

return b; returns a reference to the function b, which you can store in a variable to call later.

相關文章
相關標籤/搜索