Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
`这是Javascript回调中的`和作用域_Javascript_Jquery_Scope_This - Fatal编程技术网

`这是Javascript回调中的`和作用域

`这是Javascript回调中的`和作用域,javascript,jquery,scope,this,Javascript,Jquery,Scope,This,我将直接显示代码: disable: function(e){ that = this; var haha = this; $.post(url, function(){ console.log(this);// (why ajax object here? console.log(that);// (I understand this one works console.log(haha);// ReferenceErro

我将直接显示代码:

disable: function(e){
    that = this;
    var haha = this;
    $.post(url, function(){
        console.log(this);// (why ajax object here?
        console.log(that);// (I understand this one works
        console.log(haha);// ReferenceError 
    })
}
我在这里感到困惑的是:

  • 为什么回调中的
    这个
    不引用外部的?我认为回调中的
    这个
    遵循默认的绑定角色
  • 为什么
    哈哈
    不指的是
    呢?我想当
    哈哈
    在本地范围内找不到时,它会转到外部范围
  • 我知道使用
    不是一个好方法。这就是为什么我尝试了
    哈哈
    ,但失败了。

    我认为您正在尝试从控制台访问这些值。。。在这种情况下,
    haha
    将不起作用,因为它是函数的局部变量,您已经创建了
    作为全局变量(因为没有使用
    var


    但这是一种错误的模式,因为其他脚本可能会在ajax请求完成之前修改
    的值。

    回调函数在不同的范围内运行,因此它引用它,而不是定义它的地方。您可以使用bind、apply或调用函数来绑定它。


    因此,这是指其他内容,但由于它是在声明函数的作用域中声明的,因此它仍然存在。

    问题1的答案是:因为您可以在Javascript中随意地重新绑定它,而jQuery恰好适用于
    jQuery.post()
    ,如下所述:

    所有回调中的
    this
    引用是传递给设置中
    $.ajax
    上下文
    选项中的对象;如果未指定
    context
    这是对Ajax设置本身的引用

    一般来说:您可能永远都不应该依赖Javascript库来而不是重新绑定
    。如果在嵌套回调中需要它的值,只需保存它。在不同的命名变量中,或使用:

    JSFIDLE上的示例:。值得一提的是,为了可读性,我更喜欢使用一个单独的变量,因为你可以给它一个描述性的名称,
    这个
    没有反弹,而且你已经有效地重新分配了回调的一个参数,不会在其适用的块之后一直隐藏


    至于你的第二个问题,我无法复制,请看我的第二把小提琴:。正如其他人所说,根据您的屏幕截图,您可能实际上没有从
    控制台.log()
    本身获得
    引用错误。

    尝试使用
    $.ajax()
    上下文
    选项来设置
    成功
    错误
    回调

    disable: function(e) {
        $.ajax({
          context: this
          , type:"POST"
          , url:url 
          , success: function() {
              console.log(this);
            }
          , error: function() {
              console.log(this)
            }
        })
    }
    

    什么是“默认绑定角色”?
    haha
    应该指同一个对象。。。。代码中还有一个bug,
    中的
    不是禁用
    方法的本地代码,其他一些脚本可能会更改其值。您也可以使用
    bind
    ,例如
    $.post(url,function(){console.log(this)}.bind(this))
    @Phil
    $.post(url,(function(){console.log(this)}.bind(this))
    ?我在回调函数中设置了一个断点,并打印出这些值。为什么哈哈不这样做?
    disable: function(e) {
        $.ajax({
          context: this
          , type:"POST"
          , url:url 
          , success: function() {
              console.log(this);
            }
          , error: function() {
              console.log(this)
            }
        })
    }