是否可以指定"此"在jQuery延迟回调? (如在Array.prototype.map中)
答案 0 :(得分:2)
$.get( "test.php" ).then(
$.proxy(this.good,this),
$.proxy(this.fail,this)
);
$.get( "test.php" ).then(
this.good.bind(this),
this.fail.bind(this)
);
答案 1 :(得分:1)
您还可以使用本机javascript将“this”绑定到传递的函数。
类似的东西:
$.get('https://jsonplaceholder.typicode.com/posts')
.then( function(response) {
console.log(this);
}.bind(this))
我对这个例子做了一个小提琴 https://jsfiddle.net/8xk0s4na/