Non-method functions and this

David Herman dherman at mozilla.com
Mon May 16 07:15:24 PDT 2011


>> How do you define "non-method"?
> 
> A function that is not invoked as method. Right now, the same kind of construct is used for both true functions and methods. I’m proposing a new construct (similar to the distinction that Python makes): a function that does not have an implicit |this| parameter.

Hm, you've lost me... Your proposal seems to have several possible interpretations:

a) A compile-time error at the definition site for a function that will ever be invoked as a function. This requires the ability to predict the future, which turns out to be kinda hard. ;-P

b) A compile-time error at the definition site for a function that is not defined inside of an object. This would be way too restrictive; programmers rightfully use the ability to define a function and then put it into objects afterwards.

    function m(x, y, z) { ... this ... }
    obj1.m = obj2.m = m;

or even

    obj.m = function(x, y, z) { ... this ... }

c) At runtime, a function invoked as a non-method gets a |this|-binding of *undefined*. This is already the semantics of ES5 strict and therefore Harmony.

d) At runtime, a function invoked as a non-method gets a dynamic ReferenceError if it tries to refer to |this|. This would just be kind of obnoxious, I think, since if the function wants to test whether it's been called as a non-method it has to do something like

    let nonMethod = false;
    try { eval("this") } catch (e) { nonMethod = true }

That seems unfortunate.

Dave



More information about the es-discuss mailing list