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

Neil Mix nmix at pandora.com
Mon Aug 20 18:15:21 PDT 2007


My experience with mere mortals (e.g. me) is that over time classes  
accumulate methods, and often no one bothers to take time to group  
static methods together in the source.  This makes it easy to  
errantly put "this" inside a static method without realizing the  
implications.  Java's compile-time catch of that problem is nice.

I see the value of referring to the class within a static method  
(other than by using the class name itself).  I also think it may be  
advisable to use an identifier other than "this".  Otherwise it's too  
easy to miss the fact that a method is static.

Another way to state this: most programmers new to JS struggle with  
figuring out what "this" refers to in a given context.  Adding yet  
another situation in which "this" could be this or that seems a bit  
risky.

On Aug 20, 2007, at 4:24 PM, Brendan Eich wrote:

> 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
>
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss




More information about the Es4-discuss mailing list