es6怎么写提升函数变量声明的方法

es5这么写可以提升函数变量声明

xxx: function(){
    foo();
    function foo() {
        console.log(123);
    }
}

请问es6该怎么写

阅读 2.1k
2 个回答
  • es6没取消函数声明提升,还原来那么写就行。
  • es6下let、const声明变量"无提升(其实有提升但是会有别的原因引起报错)"

不知是否是我理解的这样,希望能对你有所帮助!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
</body>
<script>
    var test = function() {
        foo();

        function foo() {
            console.log(123);
        }
    }
    test();
    let test1 = () => {
        (() => console.log(456))(); //其实这里直接写 console.log(456) 是一样的效果
    };
    test1();
</script>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题