3 ms·
We can think of methods in Object Oriented programming as functions with their first argument (self/this/etc.) curried. This is a very limited form of currying
by chriswarbo 13y ago
We can think of methods in Object Oriented programming as functions with their first argument (self/this/etc.) curried.
This is a very limited form of currying though, since a) only the first argument gets curried and b) in order to access these curried functions (methods), we have to qualify them with the object they're inside. This pretty much 'cancels out' any syntactic saving we might have hoped for.
For example, compare these two implementations of the same algorithm:
foo = function(this, x) { run(this, x, x); }
foo(obj, 10);
class Bar {
function bar(x) { this.run(x, x); }
}
obj.bar(10);
The "foo" implementation requires us to pass in an explicit object parameter like "obj", which is then explicitly passed along to the "run" function. However, the "bar" functions are wrapped in objects, so we must specify which one we want in order to call it (eg. "obj.bar"). Likewise, inside "bar" we must look up the "run" function inside "this", so we've not actually gained anything here; we've just shuffled the tokens around!