当我调用成员函数时,对象成员变为null

时间:2019-01-23 11:19:43

标签: javascript jquery object this

我在使用javascript处理对象的成员变量时遇到麻烦。 我有这个功能,可以在HTML中找到一个特定的表,它似乎工作得很好。但是由于某种原因,与表关联的对象的成员变量变为空...

我尝试调试,这就是我得到的。

let tables;
function find_table(s) {
    for(const table of tables){
        if(table.prefix === s){
            return table;
        }
    }
    return null;
}

function search_table(table){
    const tmp = find_table(table);
    console.log(tmp.prefix); // debug0
    tmp.search();
}

在名为“表”的对象内。

search(){
        const search_text = $("#" + this.prefix + "_search_text").val().toLowerCase();

        let item_count = 0;
        $("#" + this.prefix +"_table tr").each(function () {
            if (!$(this).hasClass("search_ignore")) {
                let count = 0;
                let show = false;
                console.log(this.prefix); // debug1
                for(const type of this.types){ // error here. this.types is undefined
                    const temp = $(this).find("td").eq(count).text().toLowerCase();
                    if(type === 0){
                        // 文字列検索
                        show = temp.includes(search_text)
                    }else if(type === 2){

...(the rest is probably irrelevant)...

因此,我调用search_table()函数,以便可以在带有字符串的表中搜索表。函数find_table()似乎工作正常。 debug0行正确返回"admin_account_list"。但是之后,调用表的search()时,由于某种原因,变量this.types变成了null。并且debug1行还返回null

我希望它具有在调用构造函数时分配的值。

如果有人帮助我了解我在这里做错了什么,我将不胜感激。

0 个答案:

没有答案