NodeJS v12.13.0 中的私有静态方法 - 语法错误
Private static methods in NodeJS v12.13.0 - syntax error
如 Mozilla 在此处的 JavaScript 参考中所述:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields#Private_static_methods
私有静态方法应该这样工作:
class Foo {
static #privateStaticMethod() {
return 42;
}
}
但是,在 NodeJS v12.13.0 中使用它时,会抛出以下语法错误:
static #privateStaticMethod() {
^
SyntaxError: Unexpected token '('
at Module._compile (internal/modules/cjs/loader.js:892:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (.../foo.js:8:14)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
查看 browser compatibility page,私有静态方法 应该 从 v12 开始支持。
这是为什么?
table 声明支持 Private Static 字段 ,而不是方法。
在节点 13.2.0
中,它在 --harmony-private-methods
标志下工作
[class] implement static private methods was added in v8 7.9
. That version of v8 was added to Node 13.2.0
使用标志,在节点 12.13.0
中,您在尝试访问方法时不会得到 SyntaxError
而是 TypeError
TypeError: Read of private field Foo from an object which did not contain the field
如 Mozilla 在此处的 JavaScript 参考中所述:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields#Private_static_methods
私有静态方法应该这样工作:
class Foo {
static #privateStaticMethod() {
return 42;
}
}
但是,在 NodeJS v12.13.0 中使用它时,会抛出以下语法错误:
static #privateStaticMethod() {
^
SyntaxError: Unexpected token '('
at Module._compile (internal/modules/cjs/loader.js:892:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (.../foo.js:8:14)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
查看 browser compatibility page,私有静态方法 应该 从 v12 开始支持。
这是为什么?
table 声明支持 Private Static 字段 ,而不是方法。
在节点 13.2.0
中,它在 --harmony-private-methods
标志下工作
[class] implement static private methods was added in v8 7.9
. That version of v8 was added to Node 13.2.0
使用标志,在节点 12.13.0
中,您在尝试访问方法时不会得到 SyntaxError
而是 TypeError
TypeError: Read of private field Foo from an object which did not contain the field