我正在使用下一个代码:
<!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>
如果我对此进行更改:
this.publicMethod = function() {
privateVar1;
debugger;
// Navigating to console and typing in: 'privateVar1' now returns 'bla'
};
这是某种Chrome错误吗?我可以正确调用privateVar1吗?