Bairdy
Bairdy2y ago

prototype dot functions. How?

How would I go about creating dot function that can be chained at the end of a number in Deno? Example: (2).toUSD() // returns $2.00
function toUSD(amount) {
// RETURN Something..
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
}).format(amount);
},
function toUSD(amount) {
// RETURN Something..
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
}).format(amount);
},
2 Replies
Beast
Beast2y ago
you would have to extend prototype of a Number, but please don't do that, you can just do toUSD(2)
ud2
ud22y ago
If https://github.com/tc39/proposal-call-this eventually gets implemented, you might be able to write
function toUSD() {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format(this);
}

2~>toUSD() // returns $2.00
function toUSD() {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format(this);
}

2~>toUSD() // returns $2.00
GitHub
GitHub - tc39/proposal-call-this: A proposal for a simple call-this...
A proposal for a simple call-this operator in JavaScript. - GitHub - tc39/proposal-call-this: A proposal for a simple call-this operator in JavaScript.