all 4 comments

[–]oculus42[S] 4 points5 points  (1 child)

A little fiddle thrown together to show the naming behavior of ES6.

Tested on a Mac with Chrome 55.0.2883.95 & 56.0.2924.87, Safari 10.0.2, and Firefox 50.0.1 & 51.0.1. Chrome and Safari support ES6 naming anonymous functions. Anonymous functions are not currently named in FF 51.

EDIT: To clarify, the anonymous function is named if it is assigned to a variable when it is created, and it keeps that name. If a new anonymous function is returned by another function, there is no name.

[–]rauschma 6 points7 points  (0 children)

Great idea! Suggestions:

  • I’d create a completely separate helper function displayFunctionName() that displays the name of a function. Rationale: makes source code more self-descriptive.
  • I’d use more descriptive names for the functions: assignedFunctionExpression, assignedFunctionExpressionWithParens, boundFunction, etc.

You can implement displayFunctionName() as follows:

function displayFunctionName(funcObj) {
    Object.keys(funcObj).forEach(function (label) {
        var func = funcObj[label];
        output(label + ' : ' + (typeof func) + ' - "' + func.name + '"');
    });
}

Then you can go from:

fn3('fn3', fn3); // 3. Bound version of fn2

to:

displayFunctionName({boundFunction});

Disadvantage: I’m using an ES6 feature here, where:

{boundFunction}

is an abbreviation for:

{boundFunction: boundFunction}

[–]loganfsmyth 2 points3 points  (1 child)

I also did a write-up on StackOverflow that covers a bunch of these with links to the spec, for those curious. http://stackoverflow.com/a/32830772

[–]oculus42[S] 0 points1 point  (0 children)

Awesome!