Consider
class Service { methodA(input: string[]): number { const resB = this.methodB(input); return resB * 2; } methodB(input: string[]): number { return input.length; } } If MethodB is a pure function, can MethodA also be considered a pure function?
What about the case, when MethodB is a pure function from an injected class?
class Service { constructor(helper:Helper){} methodA(input: string[]): number { const resB = this.helper.methodB(input); return resB * 2; } } In both cases I argued yes in my team, because both cases no side effects takes place and the input>output is deterministic.
On the other side, let alone the the fact, that MethodA was using this. inside its function body was enough for other to say no.
thiscontext as non/prototypal (and/or static) class methods in comparison to the module implementation?thisisn't really special. It can be treated as an argument to the function. Because it is. Especially if you use.call()or.apply()where you actually pass it in as argument. And a pure function should produce the same result given the same arguments - whether you dogetProp({name: "Fred"}, "name")(which returns"Fred") orgetProp.call({name: "Fred"}, "name")the result is the same. It's referentially transparent, too: first might be implemented asobj[prop]the second asthis[prop]but both can be substituted with({name: "Fred"})["name"].MethodAwas usingthis.inside its function body was enough for other to say no" - they're wrong. Only ifthisis mutated (its properties are written to), the function is no longer pure.