Method definitions in classes/objects

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Oct 8 08:54:15 PDT 2012


On Oct 8, 2012, at 7:41 AM, Erik Arvidsson wrote:

> On Sun, Oct 7, 2012 at 11:19 AM, Juan Ignacio Dopazo
> ...
>> I was under the impression that methods would desugar to a named function
>> with the same name as the property, but I checked the wiki and there is no
>> reference to named functions. Has this been discussed? Is there consensus on
>> anonymous/named?
> 
> It was agreed that methods would desugar to a named function. It
> should be in the ES6 draft under Property Definition Evaluation but I
> don't understand in which step that is described. Allen can probably
> point out where propName gets added to scope.


Concise methods are specified in section 13.3 of the current specification draft.

At a recent TC39 meeting we discussed the scoping of class names and decide that the class object (the constructor function) would use the same local binding rules for its name as is used by FunctionExpressions/FunctionDeclarations.  Named classes defined using a ClassExpression have a local binding for the class name.  NameClasses defined using a ClassDeclaration do not have such a local binding and instead capture the outer binding created by the declaration.

I don't believe that we have discussed, at a meeting, property name capture (other than WRT super) of ConciseMethod definitions.  It is however, something that I have thought about  quite a bit.  Concise methods, as currently specified, do not create a local binding for the property name whose value is the function. 

obj = {
     method () {}
};

is (approximately) equivalent to 

obj = {};
Object.defineProperty(obj,"method", {value: function() {}, enumerable: true, writeable: true, configurable: true});

rather than

obj = {};
Object.defineProperty(obj,"method", {value: function method () {}, enumerable: true, writeable: true, configurable: true});


I need to reconstruct all of the reasons for this.  One is that method "names" are not restricted to being identifiers.  They can also syntactically be strings, numbers, and at-names.  A method name binding that capture a reference to the function also complicates the transfer of a super referencing method from one object to another.

However, these issues could be dealt with and and I've always seen this as a pretty close call.  Are there strong use cases for giving methods a FunctionExpression-like binding of the property name?

Allen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121008/8c990b09/attachment.html>


More information about the es-discuss mailing list