Function becomes undefined when declaring local variable with same name

前端 未结 3 754
甜味超标
甜味超标 2021-01-16 12:45

I have declared a function in file so that it becomes global:

3条回答
  •  再見小時候
    2021-01-16 13:12

    It's because of javascript's hoisting

    function() {
            var speakService = speakService();
            return speakService;
        }
    

    Will be same as

    function() {
            var speakService; //Declaration moved to top
            speakService = speakService(); //here speakService is undefined
            return speakService;
        }
    

提交回复
热议问题