Skip to content

Instantly share code, notes, and snippets.

@anjanesh
Last active October 18, 2023 01:15
Show Gist options
  • Save anjanesh/11e0f061c040ba1656750d7ec9deb370 to your computer and use it in GitHub Desktop.
Save anjanesh/11e0f061c040ba1656750d7ec9deb370 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Getter in Vanilla JS</title>
<script>
let rate = undefined;
const get_usd2inr = async () =>
{
return fetch("https://open.er-api.com/v6/latest/USD")
.then(response => response.json())
.then(response =>
{
console.log(response['rates']['INR']);
return response['rates']['INR'];
});
}
document.addEventListener("DOMContentLoaded", async () =>
{
rate = await get_usd2inr();
});
let obj =
{
usd2inr1: rate,
get usd2inr2()
{
return rate;
}
}
</script>
</head>
<body>
Open Console but right clicking on the page and click Inspect Element.
<button onclick="console.log(obj.usd2inr1);">click 1</button>
<button onclick="console.log(obj.usd2inr2);">click 2</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment