Bobbler
Bobbler

Reputation: 643

Unexpected Transpile ES6>ES5

When transpiling this function into ES5 from ES6 (using Babel) I didn't expect it to change

var func = function(msg){
  alert(msg);
}

but it became

var func = function func(msg) {
        alert(msg);
};

Why is this and how does it affect usage of the function, if at all? Even if it doesn't affect usage, is there anything I should know? Thank you.

Upvotes: 1

Views: 75

Answers (1)

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

It doesn't affect the usage of the function as well, but it does give the function a way to reference itself.

In the following snippet, notice that I recursively call ff -- which is local only to that function, while I invoke it using func.

The upshot is: It's harmless, and you can ignore it.

var func = function ff(t) {
  if (t === 0) {
    console.log("Countdown down");
  } else {
    console.log("Counting down", t);
    ff(t - 1);
  }

};

func(10);

Upvotes: 1

Related Questions