Draft of Function.prototype.bind.

Brendan Eich brendan at mozilla.com
Mon Nov 3 19:28:28 PST 2008


On Nov 3, 2008, at 7:16 PM, David-Sarah Hopwood wrote:

> It's completely trivial, but if you want a hand-wavey demo:
>
>  function makeHTMLCollection(underlying) {
>    function collection(nameOrIndex) {
>      // I think this is how MSIE behaves:
>      return typeof nameOrIndex === 'string'
>          ? underlying.namedItem(nameOrIndex)
>          : underlying.item(nameOrIndex);
>    }
>    collection.namedItem = function(name) {
>      return underlying.namedItem(name);
>    }
>    collection.item = function(index) {
>      return underlying.index(index);
>    }
>
>    // define any other non-standard methods
>
>    // should use a getter here if the length can change
>    collection.length = underlying.length;
>
>    return collection;
>  }

You are once again missing the point: IE and (because of IE's  
dominance) other browsers have callable host objects for which typeof  
does *not* return "function". Changing these as you sketch above is an  
incompatible change that may break real web content.

There is evidence for this, because Mozilla's regexps used to report  
typeof-type "function" and so fell into logic wanting only a function,  
not a callable host object. Also, alert in the IE DOM is not a  
function, and it has typeof-type "object", but it is of course callable.

Yes, that means there is a de-facto standard: only function objects  
report typeof "function".

But the relevant de-facto standard here is that host objects may be  
callable, and JS code on the web knows this.

/be


More information about the Es-discuss mailing list