javascript - “未捕获的 TypeError XXX 未定义”在方法中但不在构造函数中
问题描述
我声明了一个这样开始的类:
class Editor {
constructor() {
this.note_text = document.getElementById("note_text");
this.note_preview = document.getElementById('note_preview');
this.cloze_type = document.getElementById("cloze_type");
this.cloze_id = document.getElementById("cloze_id");
this.span_debug = document.getElementById("span_debug");
this.card_selection = document.getElementById('card_selection');
this.mode = document.querySelector('input[name="preview_mode"]:checked');
console.log(this.mode.value);
this.note_text.oninput = this.display_update;
}
display_update() {
let mode = this.mode.value;
[...]
任何时候display_update()
被调用,我都会收到错误消息Uncaught TypeError: this.mode is undefined
。let mode = this.mode.value;
但是,console.log(this.mode.value);
声明后的行this.mode
输出正确的结果。
怎么可能this.mode
在构造函数中定义,而不是在方法中定义?
解决方案
推荐阅读
- r - 当我认为我不应该使用 pointDistance 时获得 NaN
- visual-studio - Visual Studio 中的自定义字体 OpenSans 不显示 XAML、UWP
- python - ReactJS 获取 POST 文件到 Flask 后端(不会发送数据)
- php - Laravel 正确编写中间件组
- regex - 为什么 Google 表格中的 regexreplace 和 regexextract 工作方式不同?
- linux - “NameUser”当前被进程使用
- python - 在 Windows 上卸载 distutils 安装的项目?
- python - 如何在 hsv 中选择好的上限和下限?
- amazon-web-services - AWS Lambda (RequestStreamLambda) IOStream 问题
- ios - 为什么每次模拟器运行后都会出现 SIGTERM 错误?