0

I'm having trouble with how member variables of objects work in javascript. I have this function which finds a specific table in HTML and it seems to be working just fine. But it for some reason the member variables of the object associated with the table becomes null...

I tried debugging and here is what I got.

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();
}

Inside the object called "table".

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)...

So, I call the search_table() function so I can search for a table in tables with a string. The function find_table() seems to be working fine. The debug0 line returns "admin_account_list" properly. But after that when search() of table is called, the variable this.types has for some reason, turned into null. And the debug1 line also returns null.

I expected it to have the value I have assigned when the constructor was called.

I would greatly appreciate it if someone helped me understand what I did wrong here.

Liam
  • 22,818
  • 25
  • 93
  • 157
funnypig run
  • 173
  • 1
  • 7
  • 2
    `this` refers to the `tr` element, why do you think it has a `types` member? Which object has the `types` member? – Adder Jan 23 '19 at 11:30
  • It's really not clear what you don't understand. `this.types` is undefined because you haven't defined it. What makes you think it would be defined? What are you expect `this.types` to return? – Liam Jan 23 '19 at 11:50
  • 1
    You're correct. I was able to get the intended behavior by using a local variable ```const types = this.types``` and using it inside the loop. Thank you. – funnypig run Jan 23 '19 at 11:51
  • 1
    Maybe a have a read though [How does the “this” keyword work?](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – Liam Jan 23 '19 at 11:52
  • 1
    To clarify, "types" was a member variable of another class called table. Because of my inexperience, I did not realize that "this" no longer refers to the object "table" when I use it inside a each() loop. – funnypig run Jan 23 '19 at 11:53
  • @Liam Thank you for the suggestion. I will read it. – funnypig run Jan 23 '19 at 11:54
  • FYI: this is because the each **function** creates a [new closure.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) – Liam Jan 23 '19 at 11:55
  • @Liam I see. Thank you for clarifying. – funnypig run Jan 23 '19 at 12:40

0 Answers0