question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: `prefer-private-class-fields`

See original GitHub issue

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields

Prefer private fields over the convention of underscore prefixed names.

This feature requires Node.js 12, so we cannot add it to the recommended preset until April 2021 (date of Node.js 10 obsoletion).

Does ESLint support this feature yet?

Fail

class Foo {
	static _PRIVATE_STATIC_FIELD

	_privateField

	_privateMethod() {
		return 'hello world'
	}
}

Pass

class Foo {
	static #PRIVATE_STATIC_FIELD

	#privateField

	#privateMethod() {
		return 'hello world'
	}
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sindresorhuscommented, Jan 9, 2022

@fisker I would argue having to use a _ prefix to pseudo hide properties/methods is also awkward. I don’t think lack of destructuring is awkward enough to not use private fields. It’s just a minor inconvenience. The proposal-destructuring-private thing will probably not land in Node.js until Node.js 20 at the earliest, which means packages will not be able to use it until 2025.

1reaction
fregantecommented, Dec 17, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Public and private instance fields proposal - TC39
This document attempts to integrate the public fields and private fields proposals into a single, coherent, orthogonal whole.
Read more >
Setting up ESLint to work with new or proposed JavaScript ...
Setting up ESLint to work with new or proposed JavaScript features such as private class fields. It turns out configuring ESLint to use...
Read more >
Class fields and private class members are now stage 4 ...
The basic design rule behind the private field proposal is: The presence or absence of a private field inside a class, can not...
Read more >
Private fields, public worries. JavaScript — as a language
Consequently, the proposal allows us to declare class fields inside the ... Quite often you can see a class component defined like this:...
Read more >
class - What are the differences between the private keyword ...
# -private fields provide compile-time and run-time privacy, which is not "hackable". It is a mechanism to prevent access to a member from ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found