我有这两个文件:
file1.js
在第一个上,我调用“Device”的subscribe方法传递一个名为“RemoteControl”的类实例
import { RemoteControl } from '../lib/devices'
.
.
.
this.device = new Device()
this.device._subscribe('231',RemoteControl)
file2.js
在这个文件上我有
export class Device extends Service {
constructor () {
super()
this.valid_devices_list = {uuid: [], deviceClass: {}}
}
_discover() {
this.emit('found', new this.valid_devices_list.deviceClass('231'));
console.log('Emite')
}
}
_subscribe (uuid , deviceClass) {
//uuid must be a regex for all bluetooth devices of the same type.
this.valid_devices_list = {uuid, deviceClass}
this._discover()
}
问题在于,当我运行此操作时,我收到了错误
this.valid_devices_list.deviceClass不是构造函数
所以我真的不知道如何将类实例“RemoteControl”传递给subscribe实例,然后将其保存在一个对象上,最后在discover方法上使用它并在那里调用构造函数。
对此有何帮助?提前谢谢!
修改
devices.js
export class RemoteControl {
constructor (uuid) {
this.uuid = uuid
}
_get () {
//some stuff could be here
}
}
答案 0 :(得分:1)
如果RemoteControl
是“实例”,那么您可以
var constructor = this.valid_devices_list.deviceClass.constructor;
this.emit('found', new constructor('231'));
或者您可以传递构造函数(取决于API的非显而易见的语义):
this.device._subscribe('231', RemoteControl.constructor)