我正在测试当我用var,this和global声明变量/方法时发生了什么,并且想知道什么是最好的方法。我有以下代码:javascript - private/public members/functions
myApp.ConfirmationWindow = function (temptype) {
var _type = temptype;
this.type = temptype;
type2 = temptype;
this.getType = function() {
return _type;
}
this.showConfirmationWindow = function (message) {
var a = _type; //valid
var b = this.type; //valid
var c = type2; // valid
var d = this.getType(); // valid
this.showWindow(message);
showWindow2(message);
showWindow3(message);
}
this.showWindow = function (message) {
var a = _type; //valid
var b = this.type; //valid
var c = type2; // valid
var d = this.getType(); // valid
}
showWindow2 = function (message) {
var a = _type; //valid
var b = this.type; //invalid
var c = type2; // valid
var d = this.getType(); // invalid
}
var showWindow3 = function (message) {
var a = _type; //valid
var b = this.type; //invalid
var c = type2; // valid
var d = this.getType(); // invalid
}
};
用法: myApp.ConfirmationWindow1 =新myApp.ConfirmationWindow(1); myApp.ConfirmationWindow1.showConfirmationWindow('Are you sure?');
目标是让类型变量和showWindow函数保密。从我的例子中可以看到,有很多方法可以实现这一点。推荐的方式是什么?
该模块的拇指显示模式和let语法;在这之前我都不知道! – user2769810
@ user2769810我很高兴我的答案喜欢它,如果您发现我的答案有用,请不要忘记单击upvote并使用向上箭头图标批准我的答案,并在其左侧打勾。感谢您的协作和快乐编码:) – GibboK