あなたが提供する最初の関数引数は、最初に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
が予約語であるためです。
ありがとう、パトリック。 – Latanmos
@ラタンモス:どうぞよろしくお願いいたします。 – user113716
@パトリックさらに機能があればどうしますか? 'get([fn1、fn2、fn3、...]);'のように? – Latanmos