Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.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 - Fatal编程技术网

Javascript函数作用域

Javascript函数作用域,javascript,jquery,Javascript,Jquery,我试图从jQuery ajax调用中调用实例方法a()。但是当我这么做的时候,它说这个方法没有定义。我认为这是因为a()不在$的范围内。但我认为这将是范围链的一部分。()如何在范围内 function Z() { this.x = 0 this.a = function() { return this.x; } this.b = function() { $.ajax({ ... s

我试图从jQuery ajax调用中调用实例方法a()。但是当我这么做的时候,它说这个方法没有定义。我认为这是因为a()不在$的范围内。但我认为这将是范围链的一部分。()如何在范围内

function Z() {
    this.x = 0
    this.a = function() {
      return this.x;   
    }
    this.b = function() {
        $.ajax({
            ...
            success: function(data, textStatus, xhr) {
                this.a(); //a() is not defined
            },
            ...
        }); 
    }  
}

z = new Z();
z.b();

var=this使用
那.a
this
无法从内部函数/对象获得,您必须使用
that=this
技巧

大多数人会建议像
var这样的技巧,它=这个,但我更喜欢使用函数绑定来更优雅、更清晰地实现相同的目的

创建一个本地函数
a
,它是
this。a
绑定到
this

function Z() {
    this.x = 0
    this.a = function() {
      return this.x;   
    }
    this.b = function() {
        var a = this.a.bind(this);
        $.ajax({
            ...
            success: function(data, textStatus, xhr) {
                a();
            },
            ...
        }); 
    }  
}

z = new Z();
z.b();
试试这个:

function Z() {
    var that=this;
    this.x = 0
    this.a = function() {
      return that.x;   
    }
    this.b = function() {
        $.ajax({
            ...
            success: function(data, textStatus, xhr) {
                that.a(); //a() is not defined
            },
            ...
        }); 
    }  
}

z = new Z();
z.b();

您不能在回调函数中使用“this”

function Z() {
var that=this;
this.x = 0
this.a = function() {
  return this.x;   
}
this.b = function() {
    $.ajax({
        ...
        success: function(data, textStatus, xhr) {
            that.a(); //a() is not defined
        },
        ...
    }); 
}  }

this
是调用
this.a()
时的jQuery Ajax对象。您需要这样做:

function Z() {
    this.x = 0
    this.a = function() {
      return this.x;   
    }
    this.b = function() {
        var that = this;
        $.ajax({
            ...
            success: function(data, textStatus, xhr) {
                that.a(); //a() is not defined
            },
            ...
        }); 
    }  
}

z = new Z();
z.b();

其他的解决方案几乎肯定会起作用,但有一种更巧妙、更惯用的方法可以使用jQuery实现这一点:使用:

上下文 此对象将成为所有Ajax相关回调的上下文。默认情况下,上下文是表示调用中使用的ajax设置的对象(
$.ajaxSettings
与传递给
$.ajax
的设置合并)


没有“那个”把戏,改变


试着提醒
这个
。你会发现这并不是你所期望的。这个问题,或者类似的问题,一天大约被问10次。有没有人可以在jQuery文档中添加一个或两个FAQ类型的答案,这样这些问题就可以直接问到那里?或者如果存在这种情况,请发布链接-经常!!:-)虽然剥猫皮的方法显然不止一种,但是有。如果你想同时访问两个对象呢?你说的“两个”是什么意思?哪两个对象?
Z
的实例,以及回调中通常显示的
这个
对象?如果这是实际需要,就连自编程序的忍者都误解了“上下文”。为什么这个选项不被称为bind,因为这是它所模拟的。函数的this关键字与调用或执行上下文完全无关。没什么,娜达,没什么。事实上恰恰相反——执行上下文有一个this参数,该参数是在该上下文中执行任何代码之前设置的。它完全由调用代码(或ES5中的bind)控制。它甚至可以是未定义的(ES5严格模式)或空值。好吧,所以在醒来5分钟后尝试清晰响应是个糟糕的主意。让我再试一次:您是对的,
与执行上下文不完全相同。但是,由于每个执行上下文都包含一个
ThisBinding
组件(ECMA-262§10.3:“与此执行上下文相关联的ECMAScript代码中的
this
关键字相关联的值),
this
值与执行上下文是1比1。使用“context”作为
ThisBinding
的简写是合理的,特别是因为“ECMAScript程序不可能访问执行上下文”。此外,在这个上下文中,
bind
是动词,而不是名词,所以如果有什么,我认为
bindTo
将是
$.ajax()
选项的更好名称。
$.ajax({
    ...
    context: this,
    success: function(data, textStatus, xhr) {
        this.a(); //a() is now defined :)
    },
    ...
}); 
            context: this,
            success: function(data, textStatus, xhr) {
                this.a(); //a() is not defined