glm*_*ndr 2 javascript arguments function
这是我有的:
var log = function(arg1, arg2){
console.log("inside :" + arg1 + " / " + arg2);
};
var wrap = function(fn){
return function(args){
console.log("before :");
fn(args);
console.log("after :");
}
};
var fn = new wrap(log);
fn(1,2);
Run Code Online (Sandbox Code Playgroud)
这是错的,因为我想进入控制台:
before :
inside :1 / 2
after :
Run Code Online (Sandbox Code Playgroud)
但我得到了这个:
before :
inside :1 / undefined
after :
Run Code Online (Sandbox Code Playgroud)
我如何告诉javascript args是传递给返回函数的所有参数wrap?
你可以使用apply来调用一个带有指定'this'和参数数组的函数,所以试试吧
var wrap = function(fn){
return function(){
console.log("before :");
fn.apply(this, arguments);
console.log("after :");
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
648 次 |
| 最近记录: |