英文:
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 = ()=>console.log(a);
loga() // => undefined
a ??= 1 // a is set to 1
loga() // => 1
a ??= 2 // a is not set to 2 as it is already 1
loga() // => 1
a = undefined
a ??= 2 // a is now set to 2 as it is now undefined
loga() // => 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 = "new value") : ("")
// vs:
// a ??= "new value"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论