Wildcard or asterisk (*) vs named or selective import es6 javascript

前端 未结 4 927
一个人的身影
一个人的身影 2020-12-08 09:39

Just wondering which one is the best way to use import:

import * as Foo from \'./foo\';

VS:

import { bar, bar2, bar3 } from \'./fo

4条回答
  •  Happy的楠姐
    2020-12-08 10:01

    Since, with a modern WebPack setup, the two will generate the same compiled/transpiled JS, the real value in named imports is how much more expressive it is. By naming your imports you are telling any one that opens the file which functions from a module you are going to use. An example of where this can be helpful is when writing tests, if mocking is necessary, you have an explicit list of imports to mock.

提交回复
热议问题