首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单线节点API初始化

单线节点API初始化
EN

Stack Overflow用户
提问于 2020-06-25 21:41:42
回答 1查看 31关注 0票数 0

有时,当我导入node js模块时,我需要导入然后实例化这个类。我想只用一行(这可能是nodejs的一个非常基本的问题,但我的JavaScript知识有限)。

例如:

代码语言:javascript
复制
const {JWT} = require('google-auth-library');
const client = new JWT({
    email: 'asd',
    key: 'abc',
    scopes: ['https://www.googleapis.com/auth/cloud-platform'],
  });

我发现有一个const很奇怪,它只会在我的代码中被用来实例化另一个const。我想做一些类似的事情:

代码语言:javascript
复制
const client = new require('google-auth-library')({
        email: 'asd',
        key: 'abc',
        scopes: ['https://www.googleapis.com/auth/cloud-platform'],
      });

有可能吗?正确的语法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-25 21:43:51

我不会担心这样的小事情,因为它们在性能方面无关紧要,而且你会获得更好的可读性。但是,如果你坚持这样做,你可以使用下面的代码,但我不推荐,更不用费心了

代码语言:javascript
复制
const client = new (require('google-auth-library')).JWT({
        email: 'asd',
        key: 'abc',
        scopes: ['https://www.googleapis.com/auth/cloud-platform'],
      });

。。。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62576777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档