Error: 语法错误:意外的标记 ‘??=’

huangapple go评论106阅读模式
英文:

Error: SyntaxError: Unexpected token '??='

问题

我最近尝试将我的Svelte Kit代码上传到cPanel Node.js应用程序,但出现错误,提示"??="是意外的令牌。我查看了代码,发现引发错误的行是const component = async () => component_cache ??= (await import('./layout.svelte-32c85b96.mjs')).default;。我想知道如何替换这个,因为我可以使用的Node.js版本是14。我尝试在Google上查找"??=",但什么都没有找到。

英文:

I recently tried to upload some of my svelte-kit code to a cPanel node.js app, but got an error saying that "??=" was an unexpected token. I was looking through the code, and found the line causing the error const component = async () => component_cache ??= (await import('./layout.svelte-32c85b96.mjs')).default;. I was wondering how to replace this as the node.js version I can use is 14. I tried looking up "??=" on Google but found literally nothing.

答案1

得分: 1

??= 运算符的作用是在变量未定义时将一个值赋给它。例如:

let a;
let loga = () => console.log(a);
loga() // => undefined
a ??= 1 // a 被设置为 1
loga() // => 1
a ??= 2 // a 不会被设置为 2,因为它已经是 1
loga() // => 1
a = undefined
a ??= 2 // a 现在被设置为 2,因为它现在是未定义的
loga() // => 2

所以,如果你想修复这个错误,你应该考虑使用一个二元运算符而不是 ??=

let a;
a == null ? (a = "new value") : ("")
// 相对于:
// a ??= "new value"
英文:

What the ??= (Nullish coalescing assignment) operator does is assign a value to the variable if it is undefined. For example,

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let a; let loga = ()=&gt;console.log(a);
loga() // =&gt; undefined
a ??= 1 // a is set to 1
loga() // =&gt; 1
a ??= 2 // a is not set to 2 as it is already 1
loga() // =&gt; 1
a = undefined
a ??= 2 // a is now set to 2 as it is now undefined
loga() // =&gt; 2

<!-- end snippet -->

So, if you want to fix the error then you should probably just use a binary operator instead of a ??=:

let a;
a == null ? (a = &quot;new value&quot;) : (&quot;&quot;)
// vs:
// a ??= &quot;new value&quot;

huangapple
  • 本文由 发表于 2023年7月13日 09:43:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定