是否可以在 jQuery 延迟回调中指定 "this"?
Is it possible to specify the "this" in jQuery Deferred callbacks?
是否可以在 jQuery 延迟回调中指定 "this"?
(比如 Array.prototype.map)
$.get( "test.php" ).then(
$.proxy(this.good,this),
$.proxy(this.fail,this)
);
$.get( "test.php" ).then(
this.good.bind(this),
this.fail.bind(this)
);
您还可以使用本机 javascript 将 'this' 绑定到传递的函数。
类似于:
$.get('https://jsonplaceholder.typicode.com/posts')
.then( function(response) {
console.log(this);
}.bind(this))
我用这个例子做了一个fiddle
https://jsfiddle.net/8xk0s4na/
是否可以在 jQuery 延迟回调中指定 "this"? (比如 Array.prototype.map)
$.get( "test.php" ).then(
$.proxy(this.good,this),
$.proxy(this.fail,this)
);
$.get( "test.php" ).then(
this.good.bind(this),
this.fail.bind(this)
);
您还可以使用本机 javascript 将 'this' 绑定到传递的函数。
类似于:
$.get('https://jsonplaceholder.typicode.com/posts')
.then( function(response) {
console.log(this);
}.bind(this))
我用这个例子做了一个fiddle https://jsfiddle.net/8xk0s4na/