How do I write a named arrow function in ES2015?

前端 未结 8 890
余生分开走
余生分开走 2020-11-22 16:29

I have a function that I am trying to convert to the new arrow syntax in ES6. It is a named function:

function sayHello(name) {
    console         


        
8条回答
  •  一向
    一向 (楼主)
    2020-11-22 17:01

    If by 'named', you mean you want the .name property of your arrow function to be set, you're in luck.

    If an arrow function is defined on the right-hand-side of an assignment expression, the engine will take the name on the left-hand-side and use it to set the arrow function's .name, e.g.

    var sayHello = (name) => {
        console.log(name + ' says hello');
    }
    
    sayHello.name //=== 'sayHello'
    

    Having said that, your question seems to be more 'can I get an arrow function to hoist?'. The answer to that one is a big ol' "no", I'm afraid.

提交回复
热议问题