Javascript 在ajax回调函数中引用外部对象
一个简化的例子:Javascript 在ajax回调函数中引用外部对象,javascript,jquery,Javascript,Jquery,一个简化的例子: // Let's create a new object function MyObject() { // } // Add some method to this object MyObject.prototype.myFunctionA = function() { // } // Another method MyObject.prototype.myFunctionB = function(arg) { // AJAX GET request
// Let's create a new object
function MyObject() {
//
}
// Add some method to this object
MyObject.prototype.myFunctionA = function() {
//
}
// Another method
MyObject.prototype.myFunctionB = function(arg) {
// AJAX GET request
$.get('script.php', { par : arg }, function(data) {
// and here in the callback function
// I need to call MyObject.prototype.myFunctionA method!
// but "this" references callback function so
// I don't know how to access MyObject here
});
}
我已在评论中解释了我的问题。我该怎么做呢?在调用$.get之前,可以在MyObject.prototype.myFunctionB中指定“var self=this”
然后可以在回调中使用别名“self”
MyObject.prototype.myFunctionB = function(arg) {
var self = this;
$.get('script.php', { par : arg }, function(data) {
alert(self);
});
}
最简单的:
// Let's create a new object
function MyObject() {
//
}
// Add some method to this object
MyObject.prototype.myFunctionA = function() {
//
}
// Another method
MyObject.prototype.myFunctionB = function(arg) {
// AJAX GET request
var me = this;
$.get('script.php', { par : arg }, function(data) {
// use me.something instead of this.something
});
}
可重用(使用范围捕获):
然后
因此,与以下注释相关的一些代码createDelegate也可以以几种不同的方式使用,其中之一是:
function createDelegate(obj, handler)
{
handler = handler || this;
return function() {
handler.apply(obj, arguments);
}
}
Function.prototype.createDelegate = createDelegate;
这允许您执行以下操作:
var someObj = {a:1, b:function() {return this.a;}};
var scopedDelegateForCallback = someObj.b.createDelegate(whateverobj)
你们也可以耍些小把戏来找父母,但这对我来说太麻烦了,我不想用自动取款机
或者,您可以这样做:
function createDelegate(handler, obj)
{
obj = obj || this;
return function() {
handler.apply(obj, arguments);
}
}
Object.prototype.createDelegate = createDelegate;
使用它:
someObj.createDelegate(someObj.b);
或者可能:
function createDelegateForMember(handlerName, obj)
{
obj = obj || this;
return function() {
obj[handlerName].apply(obj, arguments);
}
}
Object.prototype.createDelegate = createDelegateForMember;
然后
JavaScript函数“”在外部作用域中包含变量,因此您可以执行以下操作:
// Another method
MyObject.prototype.myFunctionB = function(arg) {
// Save `this` reference for use in callback.
var that = this;
// AJAX GET request
$.get('script.php', { par : arg }, function(data) {
// Now `that` holds the contents of the current `MyObject`.
// So you can call other methods.
that.myFunctionA();
// ... etc ...
});
}
我喜欢您的
createDelegate
函数,但我认为如果您将obj
参数移动到参数列表的末尾,并将其设置为可选,默认设置为this
,效果会更好。这将在最常见的用例中保存一些代码。如果不指定“this”,则使用createDelegate函数是无用的:)不,我的意思是:函数createDelegate(handler,obj){return function(){handler.apply(obj | | this,arguments)}
。并这样调用它:$.get('script.php',{par:arg},createDelegate(函数(数据){/*dosomething*/}))代码>。是的,但是'obj | | this'中的'this'指的是createDelegate返回的函数的'this',它将被设置为'window'(就这一点而言,createDelegate的'this'将被设置为'window')-TBC的可读性我打算发表另一条评论,但会把它放在我的帖子中,给我一分钟:)干净简单。回答得好!
function createDelegateForMember(handlerName, obj)
{
obj = obj || this;
return function() {
obj[handlerName].apply(obj, arguments);
}
}
Object.prototype.createDelegate = createDelegateForMember;
someobj.createDelegate("b");
// Another method
MyObject.prototype.myFunctionB = function(arg) {
// Save `this` reference for use in callback.
var that = this;
// AJAX GET request
$.get('script.php', { par : arg }, function(data) {
// Now `that` holds the contents of the current `MyObject`.
// So you can call other methods.
that.myFunctionA();
// ... etc ...
});
}