有时,当我导入node js模块时,我需要导入然后实例化这个类。我想只用一行(这可能是nodejs的一个非常基本的问题,但我的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。我想做一些类似的事情:
const client = new require('google-auth-library')({
email: 'asd',
key: 'abc',
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});有可能吗?正确的语法是什么?
发布于 2020-06-25 21:43:51
我不会担心这样的小事情,因为它们在性能方面无关紧要,而且你会获得更好的可读性。但是,如果你坚持这样做,你可以使用下面的代码,但我不推荐,更不用费心了
const client = new (require('google-auth-library')).JWT({
email: 'asd',
key: 'abc',
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});。。。
https://stackoverflow.com/questions/62576777
复制相似问题