Given the definition:
A pure function is a function which given the same input, will always return the same output and produces no side effects.
Can we consider function AmIPure
as pure function? According to definition no, but I want to make sure.
function Amount(value, currency) {
this.value = value;
this.currency = currency;
}
function AmIPure(value, currency) {
return new Amount(value, currency);
}
var foo = AmIPure(5, "SEK");
var baz = AmIPure(5, "SEK");
console.log(foo === baz); //false
It depends on the definition of "same".
If you expect strict object equality, then only functions returning scalars (e.g. numbers, booleans, ...) could ever be considered "pure".
In general, though, that's not what you really mean: you don't usually care if you get the exact same instance of an object, only if it is equal to another according to some definition, e.g.:
"HELLO"
and "HELLO"
){x:0,y:1}
and {y:1,x:0}
)[1,2,3]
and [1,2,3]
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With