在ES6中调用import之后立即执行模块 [英] Execute a module immediately after calling import in ES6

查看:1062
本文介绍了在ES6中调用import之后立即执行模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NodeJS进行开发,正在使用ES6语法中的 import 关键字。我想在调用它后立即执行。我搜索了类似的想法,但是没有什么足够有用的。

I'm working on something on NodeJS and I'm using the import keyword from the ES6 syntax.. and I want to execute immediately after calling it. I searched for similar thoughts to do that but nothing was helpful enough.

我要做的基本上是将下面的代码从CommonJS转换为ES6。

What I want to do is basically transform the following code from the CommonJS into the ES6.

// In CommonJS:
var birds = require('./birds')()

// In ES6:
import birds from './birds'()

I可以使用 const 关键字做到这一点:

I can do that using the const keyword:

import birds from './birds'
const SomethingButNotBirds = birds()

但是,我真的想知道是否有

But, I really wanna know if there's a better way to do it.

非常感谢您的帮助!

推荐答案

ES6 import语句具有声明性语法,不会像执行 require()()那样为您提供执行函数的空间。

ES6 import statement has a declarative syntax and does not give you room to execute functions just as you'd do with require()().

下面的代码是唯一有效的方法。

The code you have below is the only valid way of doing it.

import birds from './birds'
const SomethingButNotBirds = birds()

这篇关于在ES6中调用import之后立即执行模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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