JavaScript losing “this” object reference with private/public properties

后端 未结 4 1302
挽巷
挽巷 2021-01-27 12:50

I have the following error when running the page below:

\"this.testpublic is not a function\"


test = function() {
        var testprivate = function() {         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-27 13:41

    No, it shouldn't. The function merely defines scope.

    When you call foo.bar() then this (inside bar()) is foo. Since there is no explicit foo in this case, it is window by default.

    (this is handled differently when the new keyword is in play, but it isn't for that call)

提交回复
热议问题