gpt4 book ai didi

node.js - NodeJS v12.13.0 中的私有(private)静态方法 - 语法错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:51:57 24 4
gpt4 key购买 nike

如 Mozilla 的 JavaScript 引用中所述:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields#Private_static_methods

这就是私有(private)静态方法应该如何工作:

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 起应该支持私有(private)静态方法。

这是为什么?

最佳答案

该表指出支持私有(private)静态字段,而不是方法。

在 Node 13.2.0 中,它在 --harmony-private-methods 标志下工作

[class] implement static private methods在 v8 7.9 中添加。 v8 的版本是 added to Node 13.2.0

<小时/>

使用该标志,在 Node 12.13.0 中,尝试访问该方法时不会收到 SyntaxError,而是收到 TypeError

TypeError: Read of private field Foo from an object which did not contain the field
<小时/>

v8 问题: Fully implemented behind flag

关于node.js - NodeJS v12.13.0 中的私有(private)静态方法 - 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59018117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com