Javascript 将变量传递给函数上下文

Javascript 将变量传递给函数上下文,javascript,Javascript,我想知道是否有一种方法可以做到以下几点: function testFunction(a, b) { console.log(a + b, c); } testFunction(2,3); // Should print "5, undefined" testFunction.localContext.c = "foo"; testFunction(2,3); // Should print "5, foo" 事实上,我希望能够设置函数上下文的变量 谢谢 事实上,详细情况如下: f

我想知道是否有一种方法可以做到以下几点:

function testFunction(a, b) {
   console.log(a + b, c);
}
testFunction(2,3); // Should print "5, undefined"
testFunction.localContext.c = "foo";
testFunction(2,3); // Should print "5, foo"
事实上,我希望能够设置函数上下文的变量

谢谢

事实上,详细情况如下:

   function requestHandler(req, res) {
       function validateMsg(str) {
            // Do stuff with req.user and str and so on
       }
       var msg = req.body.message;
       if (validateMsg(msg)) {
           send(msg);
       }
   }

我的问题是,我想将函数“validateMsg”移到顶部,以便在其他请求处理程序中使用它。但如果我这样做,我就会失去它的localScope。这就是为什么我想改变它。

不,但对象是用来解决这个问题的。如果有一点功能需要携带数据,那么对象就是最好的选择,事实证明JavaScript函数本身就是对象

function testFunction(a, b) {
    console.log(a + b, testFunction.c);
}

testFunction(2,3); // Prints "5, undefined"
testFunction.c = "foo";
testFunction(2,3); // Prints "5, foo"
编辑:使其独立于函数名称:

function setCAndCall(f, c) {
    f.c = c;
    f(2,3);
}

setCAndCall(testFunction, "foo"); // prints "5, foo"
// and you can do that with any function, not just testFunction

然而,
f
的定义必须引用自身,就像
testFunction
在其主体中引用
testFunction.c
一样。如果您希望所有内容都独立于函数nae,那么此时您最好使用适当的对象和
this
关键字,正如其他答案中所指出的那样。

否,但对象是用来解决该问题的。如果有一点功能需要携带数据,那么对象就是最好的选择,事实证明JavaScript函数本身就是对象

function testFunction(a, b) {
    console.log(a + b, testFunction.c);
}

testFunction(2,3); // Prints "5, undefined"
testFunction.c = "foo";
testFunction(2,3); // Prints "5, foo"
编辑:使其独立于函数名称:

function setCAndCall(f, c) {
    f.c = c;
    f(2,3);
}

setCAndCall(testFunction, "foo"); // prints "5, foo"
// and you can do that with any function, not just testFunction

然而,
f
的定义必须引用自身,就像
testFunction
在其主体中引用
testFunction.c
一样。此时,如果您希望所有内容都独立于函数nae,则最好使用适当的对象和
this
关键字,如其他答案所述。

另一种方法是定义封装第三个元素的对象:

var测试={
c:未定义,
fn:功能(a、b){
console.log(a+b,this.c);
}
}
测试fn(2,3);//打印“5,未定义”
试验c=99;

测试fn(2,3);//打印“599”
另一种方法是定义封装第三个元素的对象:

var测试={
c:未定义,
fn:功能(a、b){
console.log(a+b,this.c);
}
}
测试fn(2,3);//打印“5,未定义”
试验c=99;

测试fn(2,3);//打印“599”
我知道这不完全是你想要的,但你认为你能从中有所收获吗
arguments
是函数上下文中的数组变量,它将保存传递的所有参数,您甚至不必声明变量

function testFunction() {
  var c = [];
  for(var i in arguments){
    c.push(arguments[i]); // you can do anything with the number of variables or iterations
  }
  console.log(c.join(','));
}
testFunction(2,3); // Should print "2,3"
testFunction(2,3,'foo'); // Should print "2,3, foo"
testFunction('1', 2, 3,'foo', 88); // Should print "1, 2, 3, foo, 88"
编辑2:问题陈述 将fn
validateMsg()
移出fn
requestHandler()
范围

function validateMsg() {
    var str = arguments[0];
    var req = arguments[1];
    var res = arguments[2];
    // Do stuff with req.user and str and so on
}
function requestHandler(req, res) {
   var msg = req.body.message;
   if (validateMsg(msg,req,res)) {
       send(msg);
   }
}

我想你可以随心所欲地传递参数,也可以选择不传递,所以别忘了在
req
res
中添加空检查,即
arguments[1]
arguments[2]

我知道这不是你想要的,但你认为你能从中有所收获吗
arguments
是函数上下文中的数组变量,它将保存传递的所有参数,您甚至不必声明变量

function testFunction() {
  var c = [];
  for(var i in arguments){
    c.push(arguments[i]); // you can do anything with the number of variables or iterations
  }
  console.log(c.join(','));
}
testFunction(2,3); // Should print "2,3"
testFunction(2,3,'foo'); // Should print "2,3, foo"
testFunction('1', 2, 3,'foo', 88); // Should print "1, 2, 3, foo, 88"
编辑2:问题陈述 将fn
validateMsg()
移出fn
requestHandler()
范围

function validateMsg() {
    var str = arguments[0];
    var req = arguments[1];
    var res = arguments[2];
    // Do stuff with req.user and str and so on
}
function requestHandler(req, res) {
   var msg = req.body.message;
   if (validateMsg(msg,req,res)) {
       send(msg);
   }
}

我想您可以随意传递参数,也可以选择不传递,因此不要忘记在
req
res
中添加空检查,即
参数[1]
参数[2]
我认为这不是解决您问题的方法,但是您询问了在函数上下文中设置变量的问题

要在函数的作用域中设置变量,不能将其分配给外部可见的属性-这不是一回事。您需要创建一个闭包(通过创建一个返回
testFunction
)的函数,或者执行以下操作,但请注意,参数的顺序很重要,因此您不能完全按照要求执行操作,而是执行非常接近的操作:

var testFunction = function(a,b,c){
  console.log(a+b, c);
 };

testFunction(2,3) // "5, undefined"

testFunction = testFunctio.bind(testFunction, 1);// now a=1 in testFunction    
testFunction(2,3); // "3, 3" , because a=1, and b,c=2,3      

// or if you don't want to alter testFunction
testFunction.bind(testFunction, 1)(2,3); 

我认为这不是问题的解决方案,但您询问了在函数上下文中设置变量的问题

要在函数的作用域中设置变量,不能将其分配给外部可见的属性-这不是一回事。您需要创建一个闭包(通过创建一个返回
testFunction
)的函数,或者执行以下操作,但请注意,参数的顺序很重要,因此您不能完全按照要求执行操作,而是执行非常接近的操作:

var testFunction = function(a,b,c){
  console.log(a+b, c);
 };

testFunction(2,3) // "5, undefined"

testFunction = testFunctio.bind(testFunction, 1);// now a=1 in testFunction    
testFunction(2,3); // "3, 3" , because a=1, and b,c=2,3      

// or if you don't want to alter testFunction
testFunction.bind(testFunction, 1)(2,3); 
我的问题是,我想将函数“validateMsg”移到顶部,以便在其他请求处理程序中使用它。但如果我这样做,我就会失去它的localScope。这就是为什么我想改变它

JavaScript有词汇范围,而不是动态范围。您无法更改函数的环境

我不明白为什么不能将
req
req.user
作为参数传递给函数。如果函数只有在有权访问请求对象或用户时才能工作,则它应该是函数参数的一部分:

function validateMsg(str, user) {
     // Do stuff with user and str and so on
}

function requestHandler(req, res) {
   var msg = req.body.message;
   if (validateMsg(msg, req.user)) {
       send(msg);
   }
}
我的问题是,我想将函数“validateMsg”移到顶部,以便在其他请求处理程序中使用它。但如果我这样做,我就会失去它的localScope。这就是为什么我想改变它

JavaScript有词汇范围,而不是动态范围。您无法更改函数的环境

我不明白为什么不能将
req
req.user
作为参数传递给函数。如果函数只有在有权访问请求对象或用户时才能工作,则它应该是函数参数的一部分:

function validateMsg(str, user) {
     // Do stuff with user and str and so on
}

function requestHandler(req, res) {
   var msg = req.body.message;
   if (validateMsg(msg, req.user)) {
       send(msg);
   }
}

如果要为不同的请求处理程序重用
validateMsg(..)
函数,则可以使用处理程序上下文本身调用此方法,通过
this