2

I encountered code that contained the # sign. What is it used for? The code looks something like this:

class someObject{
  #someMethod(){
    //do something 
  }
}
Sebastian Simon
  • 14,320
  • 6
  • 42
  • 61
Ran Marciano
  • 1,254
  • 5
  • 7
  • 25
  • @CalvinNunes - Yeah, but that answer isn't answering the question that the person posting it asked. Yes, it addresses the *title* of the question, but only if you completely ignore the *text* of the question. :-) (It's also really incomplete, and doesn't address the code above, which uses a private *method*, not a private *field*.) – T.J. Crowder Nov 23 '20 at 13:15

3 Answers3

5

It's a sigil (rather than an operator) that indicates that the member is private — in this case, a private method, but it's also used for private fields.

You can't use a private method or private field in code outside the class declaring them. For instance:

class Example {
    doSomething() {
        this.#method("from doSomething"); // <== Works
    }
    #method(str) {
        console.log("method called: " + str);
    }
}
const e = new Example();
e.doSomething();
e.#method(); // <=== FAILS
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
3

This is an experimental proposal. You can define Private JavaScript methods Using #

For more info, you can refer to the MDN docs

Class properties are public by default and can be examined or modified outside the class. There is however an experimental proposal to allow defining private class fields using a hash # prefix.


You can achieve something similar using ES5 (just for the sake of simplicity to explain), where you can simulate something like Private methods (which JavaScript doesn't have one natively.)

For example:

function someObj() { //assuming this is a class definition
  function someMethod() { //private method which is not accessible outside someObj

  }

  function init() { //initializes some methods or code, private methods can be used here
    someMethod();
  }

  return {
    init //only exposes the init method outside
  }
}

In the above, it will only expose the init method from the someObj which can be called as someObj.init(), whereas your someMethod will not be accessible outside of its parent method.

Example:

someObj.init(); //works
someObj.someMethod(); //won't be accessible
Mr. Alien
  • 140,764
  • 31
  • 277
  • 265
  • Nit: Stage 3 proposals aren't "experimental" (see the [process document](https://tc39.es/process-document/)). They're candidate proposals likely to advance to the spec soonish, in this case probably this year. – T.J. Crowder Nov 23 '20 at 13:17
  • 1
    @T.J.Crowder thank you for the info. :) I hope they approve the proposal ASAP. It's a long-awaited feature in JS :D – Mr. Alien Nov 23 '20 at 13:18
  • 1
    Thank you @Mr.Alien – Ran Marciano Nov 23 '20 at 13:24
  • The link to the private methods proposal is this one: https://github.com/tc39/proposal-private-methods. The link you’ve referenced is the general class fields proposal. Both are stage 3. I’ve edited the “experimental” wording and the link in the MDN article. – Sebastian Simon Nov 24 '20 at 04:27
1

hash is used to define private class fields

Jondi
  • 337
  • 3
  • 7