繁体   English   中英

用点表示法连接变量

[英]Concatenate variable with dot notation

request(`http://127.0.0.1:3000`)
                .post(`${this.apiVersion}${apiEndpoint})
                .set('Accept', acceptType)
                .set('Content-Type', contentType)
                .set('Authorization', authorization)

我有摩卡测试的上述要求。 我想对HTTP请求(post,get,put)使用变量requestMethod。

.post(`${this.apiVersion}${apiEndpoint})

像上面的代码一样,我想使用requestMethod变量而不是使用post来使其可配置。 如何将点符号与变量连接起来? 我用下面的代码尝试了一下,但是没有用。

'.'+requestMethod+'('+ this.apiVersion + apiEndpoint +')'

在javascript中,您可以将属性(奇怪的是,方法)作为对象数组的索引来访问。 因此,如果您这样做:

request(`http://127.0.0.1:3000`)[requestMethod](`${this.apiVersion}${apiEndpoint}`)
            .set('Accept', acceptType)
            .set('Content-Type', contentType)
            .set('Authorization', authorization)) 

它应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM