无法读取未定义的属性,同时测试是否未定义(但它已定义)?

huangapple go评论67阅读模式
英文:

Cannot read properties of undefined while testing if undefined (but it is defined)?

问题

console.log(this.variableInQuestion);
this.arrayOfObjects.each(function(thing) {
    if (this.variableInQuestion !== undefined && thing.property == this.variableInQuestion) {
        ...
    }
});
英文:

I have the following chunk of Typescript code in a function, with variableInQuestion = 0 being defined outside of the function:

console.log(this.variableInQuestion);
this.arrayOfObjects.each(function(thing) {
    if (this.variableInQuestion !== undefined && thing.property == this.variableInQuestion) {
        ...
    }
});

The problem is that the console.log line prints out exactly what I would expect (the default value of 0), but I get an error for the third line saying Cannot read properties of undefined (reading 'variableInQuestion') at [[[location of the first variableInQuestion in the third line]]]. I don't understand why it's giving me a "cannot read properties of undefined" error while I am testing to see if it's undefined.

EDIT: the .each thing is specific to the array of objects in question; that's not the issue. If I run the following...

console.log(this.variableInQuestion);
this.arrayOfObjects.each(function(thing) {
    if (0 !== undefined && thing.property == 0) {
        ...
    }
});

It works exactly as I'd expect.

答案1

得分: 1

解决这个问题的方法如下:

variableInQuestion = 0;

myFunction() {
    let placeholder = this.variableInQuestion;
    this.arrayOfObjects.each(function(thing) {
        if (placeholder !== undefined && thing.property == placeholder) {
            ...
        }
    });
}
英文:

The way to address this problem is as follows:

variableInQuestion = 0;

myFunction() {
    let placeholder = this.variableInQuestion;
    this.arrayOfObjects.each(function(thing) {
        if (placeholder !== undefined && thing.property == placeholder) {
            ...
        }
    });
}

Special thanks to Pointy for identifying the problem!

答案2

得分: 0

如果你使用 console.log 输出你的数据,你会看到:

class X {
    variableInQuestion = 123;
    arrayOfObjects = [1, 2, 3];

    example() {
        console.log(this.variableInQuestion);
        this.arrayOfObjects.forEach(function (thing) {
            console.log({
                this: this,
                thing: thing,
            })
        })
    }
}

new X().example()
// 123
// { "this": undefined, thing: 1 }
// { "this": undefined, thing: 2 }
// { "this": undefined, thing: 3 }

因为 forEach 不保留 this 值。

要么使用 Lambda 表达式:

this.arrayOfObjects.forEach((thing) => { console.log(this) })

要么保留 this 值:

let that = this;
this.arrayOfObjects.forEach((thing) => { console.log(that) })

或者在 forEach 中提供 thisArg(不知道 .each,但 Array.prototype.forEach 支持这个):

this.arrayOfObjects.forEach(function(thing){ console.log(this) }, this)
英文:

If you console.log your data, you'll see

class X {
    variableInQuestion = 123;
    arrayOfObjects = [1, 2, 3];

    example() {
        console.log(this.variableInQuestion);
        this.arrayOfObjects.forEach(function (thing) {
            console.log({
                this: this,
                thing: thing,
            })
        })
    }
}

new X().example()
// 123
// { "this": undefined, thing: 1 }
// { "this": undefined, thing: 2 }
// { "this": undefined, thing: 3 }

because forEach doesn't keep this value.

Either use lambda

this.arrayOfObjects.forEach((thing) => { console.log(this) })

, keep this value

let that = this;
this.arrayOfObjects.forEach((thing) => { console.log(that) })

, or provide thisArg into forEach (dunno about .each but Array.prototype.forEach supports that)

this.arrayOfObjects.forEach(function(thing){ console.log(this) }, this)

huangapple
  • 本文由 发表于 2023年4月6日 21:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定