I am a little more than a beginner.
While reading the EventEmitter source , I came across this interesting and, to me, elegant function:
// alias a method while keeping the correct context function alias(name) { return function aliasClosure() { return this[name].apply(this, arguments); }; }
I have two main questions:
First : why is the aliasClosure function named? Is it useful for anything but clarity? Also, is this a closure? For me, this looks like a semi-anonymous function.
Second : I rewrote this function as follows:
function alias2(name) { return this[name].bind(this); }
Is it equivalent? I think this should, since the this context is the same, and it persists in both versions.
Is there a reason to prefer one over the other?
source share