0

I have some sample code in JavaScript as below. When the button is clicked then the private method by the name of doSomething of object xyz is always called, even though a method of same name exists at the global level. I am trying to understand what's happening at the JavaScript level.

Question : In this situation, why is the private method of doSomething always getting called rather than the global method of same name?

<script>
    function doSomething() {
        alert('this is a global method available to all');
    }

    var xyz = function() {
        var x = {};
        x.FirstName = "Mike";
        x.changeSeat = function() {
            doSomething();
        }

        function doSomething() {
            alert('this is a private method');
        }

        return x;
    }();
</script>

<button type="button" onclick="xyz.changeSeat();">Private or Global method is called when same name methods exist?</button>
Sunil
  • 20,653
  • 28
  • 112
  • 197
  • 2
    [This article](http://ryanmorr.com/understanding-scope-and-context-in-javascript/) may help you understand about this topic. – 31piy Dec 31 '16 at 11:10
  • @31piy, From the article you pointed to, it makes sense. Since the method doSomething is being called from within the object xyz, JavaScript will first look for that method within the scope of object xyz and if such a method is not found, only then JavaScript it will go to scope level just outside the object xyz. – Sunil Dec 31 '16 at 11:21
  • You would have been less surprised if the inner `doSomething()` was defined first. It's not the scoping, it's *hoisting* that you are actually asking about. – Tomalak Dec 31 '16 at 11:28
  • @Tomalak, By defined first you mean defined at start of xyz function body? I tried placing it at start of xyz function body but still the private method of doSomething gets called. Alternatively, I placed the global doSomething function after the self-calling xyz function, but even then the private method is getting invoked. – Sunil Dec 31 '16 at 11:31
  • Yes, at the start, where the `var x` is. I did not say the code would behave differently. I just said you have been less surprised that it behaves the way it does. – Tomalak Dec 31 '16 at 11:38
  • So no matter whether its at start or end, the private method is always executed and not the global method. I am still unable to understand why hoisting is causing this and not scoping. It seems more logical that scoping could be causing this, but then I may be missing something that you know. – Sunil Dec 31 '16 at 11:40

1 Answers1

0

By defining doSomething inside xyz, you overwrite the global doSomething within the scope of xyz

Rudi
  • 2,987
  • 1
  • 12
  • 18
  • The term is *shadowing*. "overwrite" implies that the global variable is changed, which it isn't. – Bergi Dec 31 '16 at 13:32
  • Thanks for the feedback, English is not my main language. What term would be more appropriate? When I wrote "overwrite [...] within the scope" I meant that it does NOT happen globally, but only within that scope. – Rudi Dec 31 '16 at 13:43
  • The global variable `doSomething` is *shadowed* by the local variable of the same name in the scope of `xyz`. Other terms might be "not visible" or "obscured" (though English isn't my first language either) – Bergi Dec 31 '16 at 13:48