简体   繁体   English

“无法读取未定义的属性。”

[英]"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.

相关问题 无法读取未定义的属性“ removeClass”。 Java脚本 - Cannot read property “removeClass” of Undefined. Javascript 无法读取未定义的属性。 如何定义? - Cannot read property of undefined. How to define? “ typeError:无法读取未定义的属性“用户名”。” - “typeError: Cannot read property 'username' of undefined.” 无法读取未定义的属性“长度”。” - Cannot read property 'length' of undefined." 无法读取未定义的属性“indexOf”。 为什么是未定义的? - Cannot read property 'indexOf' of undefined. Why is it undefined? 无法读取未定义的属性。 但属性是进口 class 的 static function - Cannot read property of undefined. But property is a static function of an imported class 无法从未定义中读取属性“ 0”。 错误的Google Apps脚本 - Cannot read property “0” from undefined. Error Google Apps Script TypeError:无法读取未定义的属性“ get”。 是npm的问题吗? - TypeError: Cannot read property 'get' of undefined. Is npm the issue? 无法读取未定义的属性“ x”。 json文件中的第二级 - Cannot read property 'x' of undefined. second level in json file 无法读取未定义的属性“ handle”。 Parse.com迁移 - Cannot read property 'handle' of undefined. Parse.com migration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM