Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.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/4/sql-server-2008/3.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 为什么上课';s属性丢失?_Javascript_Jquery_Oop - Fatal编程技术网

Javascript 为什么上课';s属性丢失?

Javascript 为什么上课';s属性丢失?,javascript,jquery,oop,Javascript,Jquery,Oop,我想为我自己的对象设置,这些对象可以在附加时识别相同的对象并将其落下 function ImageSet(){ this.set = []; } ImageSet.prototype.length = function(){return this.set.length} ImageSet.prototype.process = function(imgObj){ var is_exist = false; $.each(this.set, function(i){

我想为我自己的对象设置,这些对象可以在附加时识别相同的对象并将其落下

function ImageSet(){
    this.set = [];
}

ImageSet.prototype.length = function(){return this.set.length}
ImageSet.prototype.process = function(imgObj){
    var is_exist = false;
    $.each(this.set, function(i){
        if (this.set[i].imgHash == imgObj.imgHash){is_exist = true};
    });

    if (!is_exist){
        this.set[this.set.length + 1] = imgObj;
        return true;
    }
    return undefined;
}

var imagesChoosen = new ImageSet();
所以当用户单击元素时,我创建了一个新的对象,并尝试将它附加到我的图像集中。 在第一次单击时,对象成功附加到集合,但在第二次单击时,控制台写入错误

TypeError: this.set is undefined if (this.set[i].imgHash == imgObj.imgHash){is_exist = true};  
调用ImageSet的代码是:

$('#content_table a').click(function(event){
    event.preventDefault();
    el = new Image($(this).attr('href'));
    if (imagesChoosen.process(el)){
        $(this).parent().css("border", "3px dotted orange");    
    } else {
        $(this).parent().css("border", "None");
    }
    console.log(imagesChoosen.length());
    return false;
});

我不明白。我的图像集在第一次调用后是否被销毁?

在传递给
每个
的匿名函数中:

$.each(this.set, function(i){
    if (this.set[i].imgHash == imgObj.imgHash){is_exist = true};
});
…此
的值是当前正在操作的数组的成员,而不是
imagesChoosen

要访问它,您需要将
从外部函数复制到另一个保留在范围内的变量

var is_exist = false;
var currentImageSet = this;
$.each(this.set, function(i){
    if (currentImageSet.set[i].imgHash == imgObj.imgHash){is_exist = true};
});
我认为应该是:

this.set[this.set.length] = imgObj;
我不熟悉JQuery each函数,但之前的文章会指出此上下文的更改,因此我会更改每个函数的:

$.each(this.set, function(i){
    if (this.set[i].imgHash == imgObj.imgHash){is_exist = true};
});
致:


for(var i=0;i
这意味着jquery中的当前迭代,每个迭代都会使用此类关键字产生问题;您将其视为普通数组,因此我更喜欢使用for循环

ImageSet.prototype.process = function(imgObj){
    var is_exist = false;
    for(var i=0; i<this.set.length; i++)
    {
        if (this.set[i].imgHash == imgObj.imgHash){is_exist = true};
    }

    if (!is_exist){
        this.set[this.set.length] = imgObj;
        return true;
    }
    return undefined;
}
ImageSet.prototype.process=函数(imgObj){
var为_exist=false;

对于(var i=0;i
this
$内部。每个
回调与它外部的
this
不同。将
var=this
放在each之前,然后访问
this。在回调内部设置
。这是我查看它时的想法;从事件中更改此上下文,但为什么第一次不会出现错误?i想想它第一次在此处添加元素时。设置[this.set.length+1]。在此处添加元素。设置[1]下次(发生错误时)它会检查未设置的此.set[0]。
for(var i=0;i<this.set.length;i++){
    if (this.set[i].imgHash == imgObj.imgHash){is_exist = true};
};
ImageSet.prototype.process = function(imgObj){
    var is_exist = false;
    for(var i=0; i<this.set.length; i++)
    {
        if (this.set[i].imgHash == imgObj.imgHash){is_exist = true};
    }

    if (!is_exist){
        this.set[this.set.length] = imgObj;
        return true;
    }
    return undefined;
}