两种异步方法在类上结合
我在currency.js文件上有货币类,并且我
在crurner.js
中都有main.js文件,我想使用getExchangerate()
带有2个参数的功能,<代码>货币2 ),然后我想将此功能绑定到convert(nose)
方法。我不想给出Currency1
和Currency2
参数以再次转换方法。然后,我想在main.js
中使用convert方法,例如convert(Currency1,Currency2,金额)
,但它不起作用。
class Currency{
constructor(){
}
async getExchangeRate(currency1 , currency2){
this.currency1=currency1;
this.currency2 = currency2
this.url = `https://api.exchangerate.host/convert?from=${currency1}&to=${currency2}`
const excResponse = await fetch(this.url)
const excResponseJSON = await excResponse.json()
return excResponseJSON.result
}
async convert(amount){
this.amount = amount;
let value1 = await this.getExchangeRate(currency1 , currency2)
return value1 * amount;
}
}
main.js
currency.convert("USD", "EUR", 3)
.then(response => console.log(response))
.catch(err => console.log(err))
I have Currency Class at currency.js file and i have main.js file
In currency.js
I want to use getExchangeRate()
function with 2 parameters (currency1
, currency2
) and then I want to bind this function to convert(amount)
method. I dont want to give currency1
and currency2
params to convert method again. Then I want to use convert method in main.js
like this convert(currency1, currency2, amount)
but it doesnt work.
class Currency{
constructor(){
}
async getExchangeRate(currency1 , currency2){
this.currency1=currency1;
this.currency2 = currency2
this.url = `https://api.exchangerate.host/convert?from=${currency1}&to=${currency2}`
const excResponse = await fetch(this.url)
const excResponseJSON = await excResponse.json()
return excResponseJSON.result
}
async convert(amount){
this.amount = amount;
let value1 = await this.getExchangeRate(currency1 , currency2)
return value1 * amount;
}
}
main.js
currency.convert("USD", "EUR", 3)
.then(response => console.log(response))
.catch(err => console.log(err))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如@pilchard所说,咖喱方法可以“记住”参数bcz,它返回了示波器功能。也许是这样的?
as @pilchard said, a curried method can "remember" the parameters bcz it returns a scoped function. Maybe something like this?