指定した最初の関数引数が最初に関数によって呼び出されるように見えますget()
。
呼び出されると、3つのパラメーターが呼び出しに提供されます。呼び出し内では、req
パラメーターはプロパティを割り当てることができるオブジェクトである必要があります。プロパティを割り当て、var
値を。に指定しました'Happy new year!'
。
渡す次の関数の引数は、パラメーターの呼び出しを介して呼び出され、next()
3つのパラメーターが再び呼び出しに提供されます。var
最初のパラメータは、プロパティを割り当てた最初の呼び出しに与えられたものと明らかに同じオブジェクトです。
これは(明らかに)同じオブジェクトであるため、割り当てたプロパティは引き続き存在します。
簡単な例を次に示します。http://jsfiddle.net/dWfRv/1/ (コンソールを開きます)
// The main get() function. It has two function parameters.
function get(fn1, fn2) {
// create an empty object that is passed as argument to both functions.
var obj = {};
// create a function that is passed to the first function,
// which calls the second, passing it the "obj".
var nxt = function() {
fn2(obj); //When the first function calls this function, it fires the second.
};
// Call the first function, passing the "obj" and "nxt" as arguments.
fn1(obj, nxt);
}
// Call get(), giving it two functions as parameters
get(
function(req, next) {
// the first function sets the req argument (which was the "obj" that was passed).
req.msg = 'Happy new year';
// the second function calls the next argument (which was the "nxt" function passed).
next();
},
function(req) {
// The second function was called by the first via "nxt",
// and was given the same object as the first function as the first parameter,
// so it still has the "msg" that you set on it.
console.log(req.msg);
}
);
これは非常に単純化された例であり、関数内のパラメーターが少ないことに注意してください。予約語なので変更var
したわけでもありません。msg
var