我有这个:
var MyObject = {};
MyObject.doStuff = function(someParam) {
var webdav = new Webdav("addr","port");
var handler = {
onSuccess: MyObject.Success,
onError: MyObject.Fail
}
webdav.PUT(handler, filename, options);
}
MyObject.Success = function(result) {
alert('status ' + result.status + result.statusstring);
}
我正在使用exo platform javascript库进行webdav访问(如果重要)
如果MyObject.Success
成功完成,我正在创建的处理程序将调用webdav.PUT
。如何将someParam
发送给该功能呢?
换句话说,在成功或失败的操作之后,我有兴趣根据结果对someParam
做一些事情。
答案 0 :(得分:1)
您应该查看javascript对象,并尝试在一个范围内包含代码。像这样:
var MyObject = {
var doStuff = function(someParam) {
var webdav = new Webdav("addr","port");
var handler = {
onSuccess: function(result) {success(result, someParam);},
onError: function() { fail(); }
}
webdav.PUT(handler, filename, options);
}
var success = function(result, someParam) {
alert('status ' + result.status + result.statusstring);
}
var fail = function() {}
this.doStuff = doStuff;
}
var myObj = new MyObject();
myObj.doStuff(param);
答案 1 :(得分:1)
这可能是你要找的东西: javascript callback function and parameters
或者可能: http://onemarco.com/2008/11/12/callbacks-and-binding-and-callback-arguments-and-references/
var someParam = 'foo';
var handler = {
onSuccess: function(result) {success(result, someParam);},
onError: function() { fail(); }
}
答案 2 :(得分:1)
一种简单的方法,利用JavaScript闭包:
var handler = {
onSuccess: function(result) { MyObject.Success(result, someParam); },
onError: MyObject.Fail
}