使用ES6导入语句时,是否有一种方式来防止未定义的项目? [英] When using ES6 import statement, is there a way to protect against items being undefined?

查看:159
本文介绍了使用ES6导入语句时,是否有一种方式来防止未定义的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 。有没有办法强制导入语句失败,如果我尝试导入的其中一个项目是未定义的?

One of the most frequent silly mistakes I make when using the new ES6 style import statement is that I'll have a typo in one of the keys in object destructuring. I can't think of a single instance where I'd ever want a value in a destructuring assignment to be undefined. Is there any way to force the import statement to fail-fast if one of the items I'm trying to import is undefined?

ie:

这篇关于使用ES6导入语句时,是否有一种方式来防止未定义的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆