Javascript OOP特权方法以私有参数作为输入访问私有方法

发布于 2024-11-02 10:01:31 字数 1123 浏览 0 评论 0原文

我正在尝试使用我定义的特权方法来更改私有成员。我创建了一个简单的类来为您提供当前遇到的问题的示例:

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野味少女 2024-11-09 10:01:31

您的 emptyArray 函数应该只是甚

function emptyArray(array) {
    array.length = 0;
};

至更好......

function emptyArray() {
    achievements = [];
};

按照您现在的方式,您将设置一个指向成就数组的指针以指向新数组,而使原始数组保持不变。将数组传递给函数会创建对该数组的另一个引用,而不是对原始变量的引用。

Your emptyArray function should just be

function emptyArray(array) {
    array.length = 0;
};

or even better ...

function emptyArray() {
    achievements = [];
};

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.

不及他 2024-11-09 10:01:31

您的 emptyArray() 如下所示:

function emptyArray(emptyThisArray) {
    emptyThisArray = [];
};

只需将 emptyThisArray 变量设置为新数组,并保持传入数组不变。应该是这样的:

function emptyArray() {
    achievements = [];
};

You have emptyArray() as this:

function emptyArray(emptyThisArray) {
    emptyThisArray = [];
};

That just sets the emptyThisArray variable to a new array and leaves the passed-in array unchanged. It should be this:

function emptyArray() {
    achievements = [];
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文