JavaScript private methods

前端 未结 30 2117
-上瘾入骨i
-上瘾入骨i 2020-11-22 08:16

To make a JavaScript class with a public method I\'d do something like:

function Restaurant() {}

Restaurant.prototype.buy_food = function(){
   // something         


        
30条回答
  •  天命终不由人
    2020-11-22 08:59

    You can do this now with es10 private methods. You just need to add a # before the method name.

    class ClassWithPrivateMethod {
      #privateMethod() {
        return 'hello world';
      }
    
      getPrivateMessage() {
        return #privateMethod();
      }
    }
    

提交回复
热议问题