是否可以指定"这个"在jQuery延迟回调?

时间:2016-11-03 12:27:16

标签: javascript jquery asynchronous jquery-deferred

是否可以指定"此"在jQuery延迟回调? (如在Array.prototype.map中)

2 个答案:

答案 0 :(得分:2)

jQuery's $.proxy()

$.get( "test.php" ).then( 
    $.proxy(this.good,this), 
    $.proxy(this.fail,this)
);

.bind()

$.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/