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

Lars T Hansen lth at acm.org
Tue Aug 21 08:04:39 PDT 2007


On 8/21/07, P T Withington <ptw at pobox.com> wrote:
> Surveying my code base I found 27 'static function's, 3 of which use
> 'this' to refer to the class, and one of which should not be static
> (use 'this' to refer to the instance and the programmer erroneously
> declared the function static).  So, I have some appreciation for it
> being an error to use 'this' in a static function, but I also want to
> be able to refer to the class in a static function, without having to
> use the class name.
>
> I'm using 'static function' to mean a function which is a property of
> a constructor in our es3 code base.  I would make these static
> functions in es4.  The use cases would probably be more like:
>
>    static var = function (...) {... need to refer to the class here ...};
>
> so, using the class name is not an option.

Why not, given that the function body is statically enclosed in the class?

That said, by analogy with "this function" the "fix" is probably "this
class" (not "this type" as suggested earlier, since we're just talking
about classes and there are other kinds of types.)

> I want a generic function
> that can refer to the class that it is a static property of.  Will
> that be possible?

But it's not really generic, is it?  It's statically inside a class
the way you've written it above.  I'm guessing you mean "generic" from
a code generator's point of view, and you'd like to avoid
parameterizing the generator of that function with the class name.

(On the other hand, if you're suggesting something like this:

  class A {
    public static var v;
  }
  A.v = function () { ... this class ... }

then I think probably not.)

--lars

>
> On 2007-08-20, at 21:43 EDT, Brendan Eich wrote:
>
> > 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
> >
> > _______________________________________________
> > 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