How to define private methods in a JS Class

杀马特。学长 韩版系。学妹 提交于 2020-08-27 08:33:06

问题


I'm trying to define a private method for a class to test that such a method can't be called from outside the class. However, I'm coming across an error even when I'm using the syntax as indicated in the Specification. I also checked MDN.

Here's the code for my class:

class CoffeeMachine {
  #waterLimit = 200;

  #checkWater(value) {
    if (value < 0) throw new Error("Negative water");
    if (value > this.#waterLimit) throw new Error("Too much water");
  }
}

Upon calling coffeeMachine.#checkWater();, I'm supposed to get an error indicating that such a method can't be called from outside the class, but instead, I'm getting Uncaught SyntaxError: Unexpected token '('.

What could be the reason for this?


回答1:


I think private methods (#myMethod()) and fields (#myField) are experimental features [source: developer.mozilar.org ] and at stage 3 for consideration but I managed to make it work by defining it to be as field and assigning it a function as follows;

#checkWater = (value) => {
  if (value < 0) throw new Error("Negative water");
  if (value > this.#waterLimit) throw new Error("Too much water");
}

OR

#checkWater = function(value) {
  if (value < 0) throw new Error("Negative water");
  if (value > this.#waterLimit) throw new Error("Too much water");
}

Now call it on the instance object as

coffeeMachine.#checkWater();

Let me hasten to add that, this code works in Google Chrome (1st image) but when tested in FireFox (2nd image), it did not run.

You should be okay with it hopefully!




回答2:


There are only private properties in JavaScript, no private methods. You can, however, just assign a function to a private property, like this:

class CoffeeMachine {
  #waterLimit = 200;

  #checkWater = value => {
    if (value < 0) throw new Error("Negative water");
    if (value > this.#waterLimit) throw new Error("Too much water");
  }
}



回答3:


As others have pointed out in the comments, private methods are not yet part of an official ES spec and have varying browser support.

Also as others have pointed out, you may want to rethink how much you really need private methods as opposed to just using a "don't touch this" naming convention.

However, my suggestion to you is that if you want to have private values, and the memory size of each individual instance of your class is not a concern, then just ditch the use of class altogether and instead use something like this. You will marvel at the lack of headaches from keeping track of what this is doing.

function coffeeMachine() {
    const waterLimit = 200;

    const checkWater = (value) => {
        if (value < 0) throw new Error("Negative water");
        if (value > waterLimit) throw new Error("Too much water");
    };

    const makeCoffee = (amount) => {
        checkWater(amount);

        console.log(`${amount} coffee!`);
    };

    return {
        makeCoffee,
    };
}

const maker = coffeeMachine();

maker.makeCoffee(20);
maker.makeCoffee(50);
maker.makeCoffee(201);


来源:https://stackoverflow.com/questions/58962695/how-to-define-private-methods-in-a-js-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!