3 ms·
An alternative would be the old lambda-calculus trick of just saving the function with values in the closure. function Tuple(/* args... */) { var arg
by jayferd 13y ago
An alternative would be the old lambda-calculus trick of just saving the function with values in the closure.
function Tuple(/* args... */) {
var args = arguments;
this.unpack = function(fn) {
return fn.apply(null, args);
}
}
- Olical 13y agoThe problem with that being that you redefine the unpack function every time a tuple is created. The this._NAME = VALUE; may look awful but it's more efficient. You are just relying on your users to not abuse _ prefixed variables. It's far from ideal.
- jayferd 13y agoActually, good js implementations (like v8, for example) will only create one code object shared by all the unpack functions, with separate closures assigned to each. And since closures can (usually) be statically determined, they can use an offset lookup for the closured variable in place of a hashtable. So it's not quite as inefficient as it looks :).
- Olical 13y agoHuh, I never knew that. Thanks! As long as you're in a decent environment that kind of definition becomes a lot smoother. I like it.