如何引用匿名函数的父参数 Javascript

How to reference parent parameters of an anonymous function Javascript

我想弄清楚如何引用存储在调用匿名函数的变量中的包装函数的参数。比如下面的例子。我 运行 遇到的问题是我习惯于通过 arguments 变量访问参数,但它只能看到参数 myFunc。我知道这应该是可能的,但我不知道怎么做。

var myFunc = function(arg1, arg2){//do stuff}
var myFuncWrapped = wrapper(myFunc);

myFuncWrapped('arg value1', 'arg value2');

function wrapper(func){
  //how can I reference 'arg value1' from here since arguments == myFunc?
}

正如评论所建议的那样,wrapper 应该返回一个函数,这样您就可以在 myFuncWrapped 被调用时通过 closure 捕获参数。

var myFunc = function(arg1, arg2) {
    console.log(arg1); // (For testing only) Should be"arg value1"
};

var myFuncWrapped = wrapper(myFunc);

myFuncWrapped('arg value1', 'arg value2');

function wrapper(func) {
    /* The anonymous function below is actually the one
     * being called when you invoke "myFuncWrapped" so it has the arguments you need.
     */
    return function() {
        console.log(arguments[0]); // (For testing only) Should be"arg value1"
        func.apply(this, arguments);
    }
}