9.6.2 - 'this' or 'super' in a static method

Brendan Eich brendan at mozilla.org
Mon Aug 20 14:24:39 PDT 2007


On Aug 20, 2007, at 2:14 PM, Garrett Smith wrote:

> In ES3, i use this in context of a function instance.
>
> A = function A () {
>
> }
> A.instances = { };
> A.getById = function getById( id ) {
>   return ( this.instances.hasOwnProperty( id ) && this.instances 
> [ id ] ) ||
>     ( this.instances[ id ] = new this( id ) );
> };
>
> static this?

Yes, it's natural to expect |this| in a "class static method" to be  
the class object. I've mentioned this to Jeff and we talked about the  
trade-offs with respect to outlawing |this|. The binding of |this| in  
your example does depend on how A.getById is called, and will be  
wrong if the method is extracted and called via another base object.  
But, one could argue that's a bug to fix with real class statics, not  
a reason to think |this| might be considered ambiguous.

/be




More information about the Es4-discuss mailing list