javascript - Can a function which returns a new object be considered "pure"? -
given definition:
a pure function function given same input, return same output , produces no side effects.
can consider function amipure
pure function? according definition no, want 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 definition of "same".
if expect strict object equality, functions returning scalars (e.g. numbers, booleans, ...) ever considered "pure".
in general, though, that's not mean: don't care if the exact same instance of object, if equal according definition, e.g.:
- if strings equal characters (
"hello"
,"hello"
) - if simple object equal attribute names , values (
{x:0,y:1}
,{y:1,x:0}
) - if arrays equal elements in same order (
[1,2,3]
,[1,2,3]
) - ...
Comments
Post a Comment