Javascript OOP特权方法以私有参数作为输入访问私有方法
我正在尝试使用我定义的特权方法来更改私有成员。我创建了一个简单的类来为您提供当前遇到的问题的示例:
// Constructor
function Player(name) {
// Private
var achievements = [];
function emptyArray(emptyThisArray) {
emptyThisArray = [];
};
// Privileged
this.restartGame = function() {
this.score = 0;
emptyArray(achievements);
};
this.addAchievement = function() {
achievements[achievements.length] = "Medal " + achievements.length;
};
this.getAchievements = function() {
return achievements;
};
// Public
this.name = name;
this.score = 0;
}
// Public
Player.prototype.getName = function() {
return this.name;
};
var player1 = new Player("Ben");
player1.score = 100;
player1.addAchievement();
player1.addAchievement();
player1.getAchievements();
player1.restartGame(); // restart the game
player1.score; // returns 0
player1.getAchievements(); // return an array of two achievements: ["Medal 0", "Medal 1"] (should actually return an empty array)
当我尝试执行特权方法 restartGame 时,分数将正确设置为零。但是当我尝试清除私有数组时(在本例中取得成就)。私有数组没有被清除。这样做的正确方法是什么?
I'm trying to change a private member by using a Privileged method that I defined. I created a simple class to give you an example of the current issue I'm having:
// Constructor
function Player(name) {
// Private
var achievements = [];
function emptyArray(emptyThisArray) {
emptyThisArray = [];
};
// Privileged
this.restartGame = function() {
this.score = 0;
emptyArray(achievements);
};
this.addAchievement = function() {
achievements[achievements.length] = "Medal " + achievements.length;
};
this.getAchievements = function() {
return achievements;
};
// Public
this.name = name;
this.score = 0;
}
// Public
Player.prototype.getName = function() {
return this.name;
};
var player1 = new Player("Ben");
player1.score = 100;
player1.addAchievement();
player1.addAchievement();
player1.getAchievements();
player1.restartGame(); // restart the game
player1.score; // returns 0
player1.getAchievements(); // return an array of two achievements: ["Medal 0", "Medal 1"] (should actually return an empty array)
When I try to execute the Privileged method restartGame the score will be correctly set to zero. But when I try to clear the private array (in this example achievements). The private array is not getting cleared. What is the correct way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
emptyArray
函数应该只是甚至更好......
按照您现在的方式,您将设置一个指向成就数组的指针以指向新数组,而使原始数组保持不变。将数组传递给函数会创建对该数组的另一个引用,而不是对原始变量的引用。
Your
emptyArray
function should just beor even better ...
The way you have it now, you are setting a pointer to the achievements array to point to a new array, leaving the original array intact. Passing an array to a function creates another reference to the array, not a reference to the original variable.
您的
emptyArray()
如下所示:只需将
emptyThisArray
变量设置为新数组,并保持传入数组不变。应该是这样的:You have
emptyArray()
as this:That just sets the
emptyThisArray
variable to a new array and leaves the passed-in array unchanged. It should be this: