Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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 jquery无法';无法访问对象内部的属性 函数foo(){ var a=1; 这是b=2; this.c=函数(){ 警报(a); 警惕(本节b节); $('.ei')。每个(函数(){ 警报(a); 警报(this.b);//未定义_Javascript_Jquery_Oop - Fatal编程技术网

Javascript jquery无法';无法访问对象内部的属性 函数foo(){ var a=1; 这是b=2; this.c=函数(){ 警报(a); 警惕(本节b节); $('.ei')。每个(函数(){ 警报(a); 警报(this.b);//未定义

Javascript jquery无法';无法访问对象内部的属性 函数foo(){ var a=1; 这是b=2; this.c=函数(){ 警报(a); 警惕(本节b节); $('.ei')。每个(函数(){ 警报(a); 警报(this.b);//未定义,javascript,jquery,oop,Javascript,Jquery,Oop,您需要将此绑定到函数 function foo(){ var a = 1; this.b = 2; this.c = function(){ alert(a); alert(this.b); $('.ei').each(function(){ alert(a); alert(this.b);//undefined <-- i need this to be updat

您需要将此绑定到函数

function foo(){
    var a = 1;
    this.b = 2;

    this.c = function(){
        alert(a);
        alert(this.b);

        $('.ei').each(function(){
            alert(a);
            alert(this.b);//undefined <-- i need this to be update to 3
        });
    }

}

var obj = new foo;
obj.b = 3; //update this property before call method
obj.c();
this.c=函数(){
警报(a);
警惕(本节b节);
$('.ei')。每个(函数(){
警报(a);

警报(this.b);//未定义的
this
内部
每个
都将引用集合中的当前元素。将
this
缓存到
that
并使用
that.b
。我只是尝试一下,它不起作用,并且u miss”)”;是的,您需要.bind(这个)我编辑了这篇文章,请现在检查一下。如果你的词法范围中有你需要的变量,就真的没有必要使用
。bind
-这比给你自己的副本加上别名效率低
。在
外部的
这个.c
也是完全没有必要的
this.c = function(){
    alert(a);
    alert(this.b);

    $('.ei').each(function(){
        alert(a);
        alert(this.b);//undefined <-- i need this to be update to 3
    }.bind(this));
}.bind(this);