gpt4 book ai didi

javascript - 对于原型(prototype) ajax,使用上下文相当于什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:17:08 26 4
gpt4 key购买 nike

使用 jquery,您可以像下面这样在 ajax 请求中访问 this:

$.ajax({
url: "https://example.com/some_api/",
context: this,
type: 'GET',
success: function(data)
{
//can access this inside now
}

以下调用的原型(prototype)的等价物是什么?

new Ajax.Request(url, 
{
onSuccess: function(data)
{

}
});

最佳答案

你可以使用 bind():

new Ajax.Request(url, 
{
onSuccess: (function(data)
{

}).bind(this)
})

这将导致回调内的this 成为回调外的任何this。您还可以绑定(bind)到 this 之外的对象,以在回调中获取自定义 this 对象。

关于javascript - 对于原型(prototype) ajax,使用上下文相当于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25231395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com