javascriptでreturn functionとreturn function()の違い
5528 ワード
参照https://stackoverflow.com/questions/7629891/functions-that-return-a-function-javascript
問題:唯一の違いは、リセットの関数が括弧を持つかどうかです.
入力:
出力:
入力:
出力:
回答1:
Asigning a variable to a function(without the parenthe sis)copies the reference to the function.Puting the parenthe the sis the the end of a function name、cars the function、returning the functions return value.
http://jsfiddle.net/kaleb/Las6w/
Returning the function name without
Calling the function with
問題:唯一の違いは、リセットの関数が括弧を持つかどうかです.
入力:
function a() {
alert('A!');
function b(){
alert('B!');
}
return b();
}
var s = a();
alert('break');
s();
出力:
A!
B!
break
入力:
function a() {
alert('A!');
function b(){
alert('B!');
}
return b;
}
var s = a();
alert('break');
s();
出力:
A!
break
B!
回答1:
Asigning a variable to a function(without the parenthe sis)copies the reference to the function.Puting the parenthe the sis the the end of a function name、cars the function、returning the functions return value.
http://jsfiddle.net/kaleb/Las6w/
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:Returning the function name without
()
returns a reference to the function,which can be as you've done with var s = a()
. s
now contains a reference to the function b()
、andカリン s()
is functionlly equivalent to caling 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 simiar to caling var x = b();
、but instead of assigning the return value of b()
you are returning it from the call ling 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();
cals the function b()、and returns its reult.return b;
returns a reference to the function b,which you can store in a variable to call later.