鍍金池/ 問答/HTML/ es6怎么寫提升函數(shù)變量聲明的方法

es6怎么寫提升函數(shù)變量聲明的方法

es5這么寫可以提升函數(shù)變量聲明

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

請問es6該怎么寫

回答
編輯回答
安于心

不知是否是我理解的這樣,希望能對你有所幫助!

<!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>
2018年4月18日 19:38
編輯回答
下墜
  • es6沒取消函數(shù)聲明提升,還原來那么寫就行。
  • es6下let、const聲明變量"無提升(其實有提升但是會有別的原因引起報錯)"
2017年2月8日 03:10