Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Hash -> Underscore #100

Closed
quantizor opened this issue Jul 28, 2017 · 15 comments
Closed

Hash -> Underscore #100

quantizor opened this issue Jul 28, 2017 · 15 comments

Comments

@quantizor
Copy link

I was very excited to see this proposal, but I'm not sure using a hash symbol is the right choice to mark a class variable as "private".

Typically when writing class or module methods, a common pattern is to prefix the name of the function, etc. with an underscore to denote that it is "internal". I think the concept of internal variables has good symmetry with the private variable proposal and thus an underscore is a better fit.

There is also the matter of Class#Method being an established notation pattern, which would be very confusing if the private variable prefix was also a hash symbol.

@ljharb
Copy link
Member

ljharb commented Jul 28, 2017

Please read the FAQ; it should explain it quite well.

@quantizor
Copy link
Author

I think I did, but if there's a specific segment you think is most enlightening I am happy to reread.

@bakkot
Copy link
Contributor

bakkot commented Jul 28, 2017

I think the FAQ doesn't address this question specifically.

@probablyup, the short version is,this._x already means "access public property named _x", and can't start meaning something else. The first few points in the FAQ get into why we can't have this._x mean both.

@zenparsing
Copy link
Member

Actually, it is possible to use underscores instead of hashes, but you would have to accept some downsides.

Here's how it would work:

#93 (comment)

But the basic idea is that if you declare

private _x;

in a scope, then all references to obj._x within that scope would use private name lookup semantics rather than normal property lookup semantics.

class A {
  private _x;
  // Now every occurrence of obj._x in this class will refer to the private field
}

Out of curiosity, would you be willing to accept the downside in order to get the underscore?

@corbinu
Copy link

corbinu commented Jul 28, 2017

@zenparsing I at least like this much better! I actually don't see the lack of privates shadowing as downside but an upside.

@zenparsing
Copy link
Member

@corbinu Consider the following example:

class A {
  private _x;
  static printX(obj) {
    console.log(obj._x);
  }
}

A.printX({
  _x: 1;
});

Intuitively, would you expect that to print "1" or throw a TypeError?

@quantizor
Copy link
Author

quantizor commented Jul 28, 2017 via email

@zenparsing
Copy link
Member

@probablyup Then you're gonna need that hash 😉

@quantizor
Copy link
Author

Why? The privateness should only apply to this._x

@zenparsing
Copy link
Member

@probablyup But in JS, this isn't special. It can be anything in the world via fn.call(obj).

@quantizor
Copy link
Author

We're discussing new language features, yes? Maybe this has a getter for classes that inspects the privateness of variables and throws.

@quantizor
Copy link
Author

It's too bad that caller was removed from ES5. Would have been useful.

@bakkot
Copy link
Contributor

bakkot commented Jul 28, 2017

@probablyup

Maybe this has a getter for classes that inspects the privateness of variables and throws.

Please see the FAQ.

@quantizor
Copy link
Author

Alright, reread the FAQ and answered my questions. I still don't think hash is the right symbol though, given its historical usage as a notation.

Thanks for your comments!

@bakkot
Copy link
Contributor

bakkot commented Jul 30, 2017

I still don't think hash is the right symbol though, given its historical usage as a notation.

I agree it's not ideal, but I don't think we have any other options, really.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants