[英]How to split an ES6 class definition across files cross-referencing private members?
Referencing a private member EG this.#rts()
gives the error:引用私有成员 EG
this.#rts()
给出错误:
SyntaxError: Private field ' #rts ' must be declared in an enclosing class
语法错误:私有字段“ #rts ”必须在封闭的 class 中声明
Although when that line is evaluated, the function has been assigned to an instance method and this is correctly bound.虽然在评估该行时,function 已分配给实例方法并且已正确绑定。
Is there a way to achieve this, IE to reference private members across files ?有没有办法实现这一点,IE跨文件引用私有成员?
Note: I'm using Node 13.注意:我使用的是节点 13。
Example:例子:
import {Cpu6502} from "./cpu6502.mjs";
console.log((new Cpu6502).beq());
cpu6502.mjs : cpu6502.mjs :
import {beq} from "./instructions.mjs";
export class Cpu6502 {
beq = beq // `this` is correctly bound
#rts = () => "RTS"
}
instructions.mjs :说明.mjs :
export function beq() {
return this.#rts() // If this line references a public member instead,
// it works fine and `this` is correctly bound.
}
You can use this approach but probably you want somenthing like a mixin你可以使用这种方法,但可能你想要像 mixin 这样的东西
import { lda } from './instructions.mjs'
export class Cpu6502 {
constructor() {
this.lda = lda.bind(this);
}
A = 0xFF
#rts() {
return "RTS";
}
ldx() {
return this.lda();
}
}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.