Does `{ next() { next() } }` have a recursive method?

Glen Huang curvedmark at gmail.com
Tue Dec 16 21:06:41 PST 2014


On second thought, since this is a method, directly calling the method name loses `this` value, so people might not do it very often. If recursion is needed, they might just do `this.next()`.

The `name` property being set is trivia, and `a.next.name === “next”` sounds reasonable.

So, no more complain. :)

> On Dec 17, 2014, at 12:32 PM, Glen Huang <curvedmark at gmail.com> wrote:
> 
> Got it. Thank you.
> 
> Was kinda hoping I was wrong though.
> 
> ```js
> let next = () => console.log(1);
> let a = { next() { console.log(2); next() } };
> a.next();
> ```
> 
> I guess people would incline to guess this is a recursion, since the method syntax isn’t as obvious as `{ next: function () {} }` that you are creating an anonymous function. Also from the spec, I believe `a.next.name` has the value “next” in the snippet, this looks like a (false) indication that the function created is not anonymous.
> 
>> On Dec 17, 2014, at 12:13 PM, Rick Waldron <waldron.rick at gmail.com <mailto:waldron.rick at gmail.com>> wrote:
>> 
>> 
>> 
>> On Tue Dec 16 2014 at 11:03:43 PM Glen Huang <curvedmark at gmail.com <mailto:curvedmark at gmail.com>> wrote:
>> To rephrase the problem:
>> 
>> Assuming `next` was never defined.
>> 
>> ```js
>> let a = { next() { next() } };
>> a.next();
>> ```
>> 
>> The result of running this code is runtime error or infinite recursion?
>> 
>> I tried to find the answer in the spec, but the initialization and calling of a function object is a bit overwhelming for me.
>> 
>> The evaluation of FunctionExpression contains a step "Call the CreateImmutableBinding concrete method of envRec passing name as the argument.” together with "Call the InitializeBinding concrete method of envRec passing name and closure as the arguments. I" guess that’s the steps enable directly calling function name in the function body?
>> 
>> Correct. This is defines the runtime evaluation semantics for the `next: function next() { }` part of `let a = { next: function next() { } }`.
>>  
>> 
>> And since DefineMethod doesn’t have such steps, is it correct to assume the above snippet should result in a runtime error?
>> 
>> It would be a ReferenceError, because next is not defined. 
>> 
>> Rick
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141217/2d764b14/attachment.html>


More information about the es-discuss mailing list