英文:
JavaScript function call context has valid parameters, but function doesn't get them
问题
在使用回调函数的自定义事件处理设置中,回调的调用上下文显示回调参数的有效值,但在进入函数后,参数值变为未定义。
如果重要的话,这是在使用React的Electron渲染器进程内部。
在一个纯粹的JavaScript对象中,调试器显示回调报告SerialPortEvent.Opened
行上的一切都有预期的上下文值。
// 文件:serialPort.js(没有导入)
class SerialPortEvent {
Discovered = 0
Opened = 1
Closed = 2
DataReceived = 3
Error = 4
}
class SerialPortSpec {
...
addEventListener(listener) {
this.listeners.push(listener)
}
open(baudRate) {
const baudRateInt = parseInt(baudRate, 10)
window.serialport.open(this.portInfo.path, baudRateInt)
.then(binding => {
this.baudRate = baudRateInt
this.binding = binding
// 在这里,调试器显示所有本地变量都与预期一致
this.listeners.forEach(listener => listener(SerialPortEvent.Opened))
}).catch(reason => {
this.listeners.forEach(listener => listener(SerialPortEvent.Error, reason))
})
}
...
}
...
module.exports = { SerialPortEventHub, SerialPortSpec, SerialPortEvent }
然而,在进入回调函数"portEventHandler"的开头一步后,参数值变为undefined
。我很想知道为什么!谢谢。
// 文件 SerialPortUI.jsx
import React from 'react'
import { SerialPortEventHub, SerialPortEvent, SerialPortSpec } from './serialPort'
import './serialportui.css'
class SerialPortPanel extends React.Component {
...
portEventHandler(eventType, ...data) {
// 在这里,eventType 是未定义的。这个文件确实导入了SerialPortEvent。
switch (eventType) {
...
}
}
...
}
英文:
In a custom event-handling setup using callbacks, the calling context of the callback shows valid values for the callback parameters, but the argument values are undefined after stepping into the function.
In case it matters, this is inside of an Electron renderer process which uses React.
In a vanilla JavaScript object, the debugger shows expected contextual values for everything on the line of the callback reporting SerialPortEvent.Opened
.
// file: serialPort.js (no imports)
class SerialPortEvent {
Discovered = 0
Opened = 1
Closed = 2
DataReceived = 3
Error = 4
}
class SerialPortSpec {
...
addEventListener(listener) {
this.listeners.push(listener)
}
open(baudRate) {
const baudRateInt = parseInt(baudRate, 10)
window.serialport.open(this.portInfo.path, baudRateInt)
.then(binding => {
this.baudRate = baudRateInt
this.binding = binding
// Here, the debugger shows all locals are as expected
this.listeners.forEach(listener => listener(SerialPortEvent.Opened))
}).catch(reason => {
this.listeners.forEach(listener => listener(SerialPortEvent.Error, reason))
})
}
...
}
...
module.exports = { SerialPortEventHub, SerialPortSpec, SerialPortEvent }
However, one "step into" later, at the beginning of the callback function "portEventHandler", the argument values are undefined
. I would love to know why! Thanks.
// file SerialPortUI.jsx
import React from 'react'
import { SerialPortEventHub, SerialPortEvent, SerialPortSpec } from './serialPort'
import './serialportui.css'
class SerialPortPanel extends React.Component {
...
portEventHandler(eventType, ...data) {
// Here, eventType is undefined. This file does import SerialPortEvent.
switch (eventType) {
...
}
}
...
}
答案1
得分: 1
> class SerialPortEvent {
> Discovered = 0
> Opened = 1
> Closed = 2
> DataReceived = 3
> Error = 4
> }
这不是一个合适的枚举声明。类字段将被创建在一个实例上,你在代码中访问的静态类属性 `SerialPortEvent.Opened` 确实是 `undefined`。这也是被调用的函数接收到的值。
相反,你应该使用具有属性的对象:
const SerialPortEvent = {
Discovered: 0,
Opened: 1,
Closed: 2,
DataReceived: 3,
Error: 4,
};
如果你不打算实例化(使用 `new`),永远不要使用 `class` 语法!
英文:
> class SerialPortEvent {
> Discovered = 0
> Opened = 1
> Closed = 2
> DataReceived = 3
> Error = 4
> }
That's not a proper enum declaration. Class fields will be created on an instance, the static class property SerialPortEvent.Opened
you are accessing in your code is indeed undefined
. And that's the value the called function receives.
Instead, you'll want to use an object with properties:
const SerialPortEvent = {
Discovered: 0,
Opened: 1,
Closed: 2,
DataReceived: 3,
Error: 4,
};
Never use class
syntax if you don't ever instantiate it (use new
)!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论