Function becomes undefined when declaring local variable with same name

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

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

3条回答
  •  独厮守ぢ
    2021-01-16 13:34

    Like others have said, your issue is hoisting. Just return an object literal and use the IIFE pattern.

    var myService = (function speakService() {
        var speak = function(word) {
            console.log(word);
        };
      
        return {
          speak: speak
        };
    })();
    
    myService.speak("TEST");

提交回复
热议问题