On Thu, Sep 18, 2008 at 10:41, Garrett Smith <[EMAIL PROTECTED]> wrote: > 2008/9/18 Mark S. Miller <[EMAIL PROTECTED]>: >> The Redmond mtg is fast approaching. We'd like to put out an official >> for-Redmond-mtg draft of the ES3.1 spec by then. I had volunteered to write >> the spec for Function.prototype.bind(). Long term, I think we all agree we'd >> like to see this spec and many others self-hosted in EcmaScript. However, >> the discussion of self-hosting issues makes clear that this ain't gonna >> happen in time for ES3.1. >> >> So what we need now is a spec for Function.prototype.bind() in the peculiar >> pseudo-code style -- combining the worst of COBOL and assembly language -- > > What pseudo-code style? This: > > Function.prototype.bind( context, [ preArg1 [, preArg1 [,...]]]) > > ? > >> used in the rest of the spec. Unfortunately, I won't have time between now >> and then. Would anyone care to contribute some text? Please? >> > > I'm not sure exactly what you're looking for so I've typed only a > little. Is this in the right direction:- > > ================================================ > Function.prototype.bind( context, [ preArg1 [, preArg1 [,...]]]) > > Returns a function that invokes this function. The |context| argument > determines the execution context of the returned function. Arguments > 1..n are used for values of parameter variables to this function. > > Example: > var brendan = { name : "Brendan" }; > function sayHelloTo(name) { > return "Hi " + name + ", my name is " + this.name + "."; > } > > brendansGreeting = sayHelloTo.bind(brendan); > > brendansGreeting("Mark"); > > returns > "Hi Mark, my name is Brendan." > > Methods |Function.prototype.call| and |Function.prototype.apply| do > not affect the |this| value of a bound function. Thus: > > brendansGreeting.call(null, "Mark"); > > returns > "Hi Mark, my name is Brendan." > > [insert_algorithm_steps_here] > > The |length| property of Function.prototype.bind is 1. > > ================================================ > A self-hosted version: > /** > * @param {Object} context the |this| value to be used. > * @param {arguments} [1..n] optional arguments that are > * prepended to returned function's call. > * @return {Function} a function that uses |context| as the this > * value for the returned function and arguments 1..n for values > * of parameter variables to this function. > */ > Function.prototype.bind = function(context) { > var fn = this, > ap, concat, args, > isPartial = arguments.length > 1; > // Strategy 1: just bind, not a partialApply > if(!isPartial) { > return function() { > if(arguments.length !== 0) { > return fn.apply(context, arguments); > } else { > return fn.call(context); // faster in Firefox. > } > }; > } else { > // Strategy 2: partialApply > ap = Array.prototype, > args = ap.slice.call(arguments, 1); > concat = ap.concat; > return function() { > return fn.apply(context, > arguments.length === 0 ? args : > concat.apply(args, arguments)); > }; > } > };
There is really no point in exposing the two different cases in a spec. A spec should describe the semantics, not the implementation. > ================================================ > > ? > > Garrett > >> -- >> Cheers, >> --MarkM >> >> _______________________________________________ >> Es-discuss mailing list >> Es-discuss@mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> > _______________________________________________ > Es-discuss mailing list > Es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -- erik _______________________________________________ Es-discuss mailing list Es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss