gpt4 book ai didi

javascript - 使用 ES6 import 语句时,有没有办法防止未定义的项目?

转载 作者:可可西里 更新时间:2023-11-01 01:43:41 25 4
gpt4 key购买 nike

import {
foobar1,
foobar2,
foobor3, //typo! this key doesn't exist in the module.

} from './module_file.js'


console.log(foobar1, foobar2, foobar3) //EXPLODES

我在使用新的 ES6 样式导入语句时犯的最常见的愚蠢错误之一是我在对象解构中的一个键中有错字。我想不出有哪个实例是我希望解构赋值中的值是 undefined 的。如果我尝试导入的其中一项未定义,是否有任何方法可以强制导入语句快速失败?

即:

 import {
doesntExistInModule //EXPLODE NOW! 🔥🔥🔥
} from './module_file.js'

最佳答案

  • There is no hook allowing a module to run some code before its dependencies load. This means that modules have no control over how
    their dependencies are loaded.
  • There is no error recovery for import errors. An app may have hundreds of modules in it, and if anything fails to load or link, nothing runs. You can’t import in a try/catch block. (The upside here is that because the system is so static, webpack can detect those errors for you at compile time.)

更多详情,read it out

关于javascript - 使用 ES6 import 语句时,有没有办法防止未定义的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36805664/

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