Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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:class Property在从方法调用时未定义_Javascript_Jquery_Methods_Scope - Fatal编程技术网

JavaScript:class Property在从方法调用时未定义

JavaScript:class Property在从方法调用时未定义,javascript,jquery,methods,scope,Javascript,Jquery,Methods,Scope,我想将锚的所有ID(包含在网页中)存储到Link对象数组中。我不是一个JS程序员,所以也许我缺少一些基础知识,但谷歌搜索和尝试实现为他人工作的解决方案对我没有帮助(比如绑定竞赛、胖箭等等)。 我所了解的是,我的这个对象在错误的范围内,它包含了所点击链接的完整路径(href),但在我使用这个广告的上面只有三行,它的效果与预期一样:$。每个(this.internal_aol,function(index,element){…} 以下是JS代码: // Link object class Link{

我想将锚的所有ID(包含在网页中)存储到
Link
对象数组中。我不是一个JS程序员,所以也许我缺少一些基础知识,但谷歌搜索和尝试实现为他人工作的解决方案对我没有帮助(比如绑定竞赛、胖箭等等)。 我所了解的是,我的
这个
对象在错误的范围内,它包含了所点击链接的完整路径(
href
),但在我使用这个广告的上面只有三行,它的效果与预期一样:
$。每个(this.internal_aol,function(index,element){…}

以下是JS代码:

// Link object
class Link{
    anchor = null;

    constructor(anchor){
        this.anchor = anchor;
    }

    getHref(){
        return $(this).attr("href");
    }

    myToString(){
        console.log(this);
    }
}

// Object containing an array of Links and some methods to manage them
class ArrayOfLinks{

    constructor(selector){
        this.selector = selector;
        this.anchorOnClick = this.anchorOnClick.bind(this);
    }

    fillArrWithLinks(){
        var tmp_aol = new Array();  
        $(this.selector).each(function(){
            console.log("Pushing " + $(this).attr("href"));
            tmp_aol.push($(this));      
        });

        this.inner_aol = tmp_aol;
        //this.selector = selector;
        //return tmp_aol;
    }

    myToString(){
        console.log("Inner AOLs " + this.inner_aol.toString());
    }

    printAllElements(){
        $.each(this.inner_aol, function(index, element){
            console.log("printAllElements -> " + $(element).attr("href"));
        });
    }

    anchorOnClick(){
        $.each(this.inner_aol, function(index, element){
            $(element).click(function(e){
                e.preventDefault();

/* *****************************
************************************************************************
the problem is in the following line when using this*/
                console.log("Link to div: " + $(element).attr("href") + " position into inner_aol: " + $.inArray($(element).attr("href"), this.inner_aol) + " my this is: " +this);
            });
        })
    }

    isAllowed(clicked_element){
        if(($(clicked_element).hasClass("deactive")) && (true)){
            console.log("this step is allowed");
        }
    }

}


l = new Link("test");
aol = new ArrayOfLinks(".link");
aol.fillArrWithLinks();
aol.myToString();
aol.printAllElements();
aol.anchorOnClick();

将单击回调绑定到类引用,如下所示:

anchorOnClick(){
    var thisRef = this;
    $.each(this.inner_aol, function(index, element){
        $(element).click((function(e){
            e.preventDefault();

            /* *****************************
            ************************************************************************/
            console.log("Link to div: " + $(element).attr("href") + " position into inner_aol: " + $.inArray($(element).attr("href"), this.inner_aol) + " my this is: " +this);
        }).bind(thisRef));
    })
}

将单击回调绑定到类引用,如下所示:

anchorOnClick(){
    var thisRef = this;
    $.each(this.inner_aol, function(index, element){
        $(element).click((function(e){
            e.preventDefault();

            /* *****************************
            ************************************************************************/
            console.log("Link to div: " + $(element).attr("href") + " position into inner_aol: " + $.inArray($(element).attr("href"), this.inner_aol) + " my this is: " +this);
        }).bind(thisRef));
    })
}