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))