On Wed, Aug 31, 2011 at 10:52 AM, Lasse Reichstein < [email protected]> wrote:
> On Tue, Aug 30, 2011 at 11:46 PM, David Bruant <[email protected]> > wrote: > >> Le 30/08/2011 21:59, Lasse Reichstein a écrit : >> >> A reliable .call could probably also achieve the same. >> >> A reliable .call could be achieved by composing a reliable .bind and the >> function call syntax. >> > > True. The Bind operation is the currying of the Call operation. > > Ah, that got me thinking. I can do > var CallFunction = Function.prototype.call.bind(Function.prototype.call); > since bind does give a different way to set the this-object for a call. > This can be done once, before anybody gets to mangle the builtins, and can > be stored for afterwards. Excellent! > On Wed, Oct 5, 2011 at 10:17 AM, Lasse Reichstein < [email protected]> wrote: > (On the other hand, it's actually a fun experience to write Javascript code > that can't be affected by a modified environment - the same way three-legged > races are fun :) > > On Wed, Oct 5, 2011 at 12:23 PM, Andrea Giammarchi < [email protected]> wrote: > dunno how we ended up here but I would rather use this approach in a > private scope: > > var > // used to trap function calls via bind > invoke = Function.call, > // normal use cases > bind = invoke.bind(invoke.bind), > apply = bind(invoke, invoke.apply), > call = bind(invoke, invoke) > ; > > var hasOwnProperty = bind(invoke, {}.hasOwnProperty); > > hasOwnProperty({key:1}, "key"); // true > call([].slice, [1,2,3], 1); // 2,3 > apply([].slice, [1,2,3], [1]); // 2,3 > > Regards, > Andrea Giammarchi > Since some implementations of Function.prototype.bind are still a bit rusty, I thought about how to do this using just call and apply. Here's what I came up with: var builtInApplyFn = Function.prototype.apply; var builtInCallFn = Function.prototype.call; var builtInSliceFn = Array.prototype.slice; builtInApplyFn.reallyCall = builtInCallFn; Function.prototype.apply = function(thisArg, argArray) { return builtInApplyFn.reallyCall(this, thisArg, argArray); }; function apply(func, thisArg, argArray) { return builtInApplyFn.reallyCall(func, thisArg, argArray); } function call(func, thisArg, var_args) { var args = builtInApplyFn.reallyCall(builtInSliceFn, arguments, [2]); return builtInApplyFn.reallyCall(func, thisArg, args); } function method2Function(meth) { return function(self, var_args) { var args = builtInApplyFn.reallyCall(builtInSliceFn, arguments, [1]); return builtInApplyFn.reallyCall(meth, self, args); }; } var hop = method2Function(Object.prototype.hasOwnProperty); var slice = method2Function(builtInSliceFn); var concat = method2Function(Array.prototype.concat); The name "method2Function" is just awful but I haven't thought of a better one. It's a hard thing to name. Suggestions appreciated. -- Cheers, --MarkM
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

