Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在作用域中的另一个函数中访问我的变量_Javascript - Fatal编程技术网

Javascript 如何在作用域中的另一个函数中访问我的变量

Javascript 如何在作用域中的另一个函数中访问我的变量,javascript,Javascript,如何在其他函数中传递相同的参数。我听说在闭包中,我们可以访问其上下文之上的变量 如果arg被声明为全局变量,我看不出问题出在哪里。 如果没有,为什么不从pm.view.someFunction中调用pm.view.otherFun?闭包意味着函数可以使用其外部作用域中的变量。下面是一个例子: (function(){ pm.view.someFunction(arg) { arg is used here. }

如何在其他函数中传递相同的参数。我听说在闭包中,我们可以访问其上下文之上的变量

如果arg被声明为全局变量,我看不出问题出在哪里。
如果没有,为什么不从
pm.view.someFunction
中调用
pm.view.otherFun

闭包意味着函数可以使用其外部作用域中的变量。下面是一个例子:

(function(){    
        pm.view.someFunction(arg) {
          arg is used here.
        }    

        pm.view.otherFun(){
          how can i pass the same arg here too
        }    
})();
test
strFunc
)返回的函数是一个闭包。它在局部变量
str
周围“关闭”
str
在strFunc之外声明,但由于它在同一范围内,所以可以访问它


在您的示例中,只有两个函数(其中一个接受
arg
参数)在同一范围内
arg
仅在
someFunction
的作用域中,
otherFun
无法访问它,除非它作为参数传递,或者
arg
是在函数之外声明的,比如
str
是如何在
strFunc
之前声明的。

确定吗?这不是有效的javascript。如何调用
someFunction
otherFun
?你不能同时传递它们吗?
arg
?顺便说一句:
pm.view.otherFun(){
不是有效的JavaScript。
function test(){
   var str = 'Hello',
   strFunc = function(){
     var s = str + ' world!';
     return s;
   };
   return strFunc;
}
var t = test();
console.log(t()); // Hello world!