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

Brendan Eich brendan at mozilla.org
Mon Aug 20 18:43:27 PDT 2007


Yeah, that's Jeff's position. It certainly is true that this-binding  
is non-trivial because dynamic in ES1-3. In ES4 for any function in a  
class, it's fixed. The current proposal outlaws it in static methods  
(not sure where this is written down, but like Fox Mulder, I want to  
believe), and specifies this-binds-to-object-from-which-method-was- 
extracted-or-called for instance methods.

We could let this alone, and say "use the class name" for static  
methods, for sure. Or we could add "this class". Comments?

/be

On Aug 20, 2007, at 6:15 PM, Neil Mix wrote:

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




More information about the Es4-discuss mailing list