没有必要通过考试
myTestSubject
作为参数,因为它在内部范围内。
下面是一个有注释的示例,可以帮助您理解。
function test() {
//start of functions test scope
var myTestSubject = "hello"; // i want to pass in this variable into the subject
var two = 'not two';
console.log(two)
async.waterfall([
function(callback) {
// this is the scope of function(callback)
// but this scope is inside of function test() scope
// so everything that is above here you can use
console.log(myTestSubject)
//one is in this scope so its not available in the next function
// you will have to pass it as argument
var one = 'one';
two = 'two'
callback(null, one, 'three');
},
function(arg1, arg2, callback) {
// every function has its own scope and you can also access the outer scope
// but you cant access the sibling scope
// you will have to pass it as argument
// arg1 now equals 'one' and arg2 now equals 'three'
console.log(two)
callback(null, 'three');
},
function(two, callback) {
// in this scope you have a two variable so two is 'three'
console.log(two)
callback(null, 'done');
},
function(arg1, callback) {
// in this scope two is 'two'
console.log(two)
//but if you do
var two = 'three';
// now you have 'three' into two
callback(null, 'done');
},
function(arg1, callback) {
// in this scope you dont two is 'two'
console.log(two)
callback(null, 'done');
}
], function(err, result) {
// result now equals 'done'
});
//end of function test scope
}