为什么在Chrome控制台标签中未定义私有变量?

时间:2019-08-02 14:08:34

标签: javascript google-chrome

我正在使用下一个代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <script src="jquery-3.3.1.js"></script>
    <script type="text/javascript">
    (function ($) {
        $.fn.credentialsDialog = function () {

            var instance = this;
            var privateVar1 = this.find("#test1").data("bla");

            this.initialize = function () {
                return this;
            };

            this.publicMethod = function() {
              debugger;      
              // When I hit here, I navigate to console and type in:
              // 'privateVar1', which throws undefined ??       
            };

            return this.initialize();
        }
    })(jQuery);


  $(document).ready(function() {    
     $("#test1").data("bla", "foo");

     var a = $("#test").credentialsDialog();
     a.publicMethod();

  });
    </script>
  </head>

  <body>
    <div id="test">Test
       <div id="test1"></div> 
    </div>
  </body>
</html>

enter image description here

如果我对此进行更改:

this.publicMethod = function() {
  privateVar1;
  debugger;      
  // Navigating to console and typing in: 'privateVar1' now returns 'bla'
};

enter image description here

这是某种Chrome错误吗?我可以正确调用privateVar1吗?

0 个答案:

没有答案