[英]"Cannot read property of undefined."
I have some simple code that I've written in Python. It's working.我有一些我在 Python 中编写的简单代码。它正在运行。 Now I'm trying to port it to JavaScript. The code is here .
现在我正在尝试将其移植到 JavaScript。代码在这里。 I'm getting this error:
我收到此错误:
sketch.js:7 Uncaught TypeError: Cannot read properties of undefined (reading 'compression_ratio')
at w (sketch.js:7:39)
at ifsp (sketch.js:37:13)
at draw (sketch.js:54:14)
at p5._main.default.redraw (p5.js:71990:27)
at _draw (p5.js:64140:25)
What's going wrong here?这里出了什么问题?
The error is here (Starting line 28).错误在这里(起始行 28)。
if (prob_sum > r) {
attractor_index = j - 1;
} else {
attractor_index = j;
}
If r
is smaller than 1/3
(probability value that you have defined in line 51) you will decrement attractors_index
using j - 1
and if that happens in the first iteration attractors_index
will be -1
as j = 0
.如果
r
小于1/3
(您在第 51 行中定义的概率值),您将使用j - 1
递减attractors_index
,如果在第一次迭代中发生这种情况,则attractors_index
将为-1
,因为j = 0
。 In that case attractors[attractors_index]
will return undefined
and you call w()
with undefined
instead of the object you expect, hence attractors.compression_ratio
will not have a property compression_ratio
and the error is thrown.在这种情况下,
attractors[attractors_index]
将返回undefined
并且您使用undefined
而不是您期望的 object 调用w()
,因此attractors.compression_ratio
将没有属性compression_ratio
并抛出错误。
You can check/ reproduce that easily by manually setting r
to >
or <
1/3
.您可以通过手动将
r
设置为>
或<
1/3
来轻松检查/重现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.