Comments (6)
Private accessors can be useful for debugging reasons. If we have a private field and want to intercept it's modifications we can change it to a private getter / setter. If we don't have private accessors we would have to rename the variable to debug it.
from proposal-private-methods.
I don't really see the point of allowing private accessors, accessors in general seem to only really be useful to do one of a couple things:
- Preventing data duplication while retaining an ergonomic API e.g.:
const person = {
firstName: "Bob",
lastName: "Smith",
get fullName() {
return this.firstName + this.lastName
}
}
- The Liskov substitution principle:
const yesImReallyADOMNodeTrustMe = {
style: {
set backgroundColor() {
// Do some rendering to some fake DOM
}
...
}
...
}
The first reason might apply internally to the class, but the Liskov substitution principle obviously doesn't apply to private fields.
It could apply though if friend classes were possible e.g.:
// syntax for friend fields irrelevant
#x
#y
class Point2D {
// syntax irrelevant
outer #x
outer #y
constructor(x, y) {
this.#x = x
this.#y = y
}
transformBy(otherPoint) {
return new Point2D(this.#x + otherPoint.#x, this.#y + otherPoint.#y)
}
class Point3D {
outer #x
#optimizedPoint
constructor(x, y, z) {
this.#optimizedPoint = engine3D.createPoint(x, y, z)
}
get #x() {
return this.#optimizedPoint.getX()
}
}
Given that friend private fields aren't part of the current proposal I don't really see any point in having private accessors unless there's some concrete examples of where ergonomics are really improved with it.
If they are desirable for friend classes then they can always be part of whatever proposal adds friend classes.
from proposal-private-methods.
At committee this seemed to come down pretty firmly on the side of "yes", if I remember correctly -
lots of people described use cases, often involving refactoring.
from proposal-private-methods.
I want to leave this open for future feedback for now. In some ways, it's easier to say "yes" than "no" to complexity.
As described in the unified class features explainer, one application would be for decorators which turn a private field into a private accessor, but we could delay private accessors for now until we also have decorator integration.
Another piece of motivation is orthogonality: Anything you can do publicly should be possible to do privately, so you don't have to think about, "I'm changing this between public and private; can I do that with this sort of class element?" The answer is always, yes.
from proposal-private-methods.
@littledan Regarding orthogonality the way private fields are spec-ed there's going to be things public fields can do that private ones necessarily can't. For example deletion (delete this.#x
) and dynamic access (this[privateName]
) can't be directly changed to some private field equivalent.
from proposal-private-methods.
We've concluded that we do want to add private accessors; see further motivation in #27.
from proposal-private-methods.
Related Issues (20)
- Using mask, masked or local when declaring private methods HOT 8
- Typo on spec.html HOT 2
- out of sync: PrivateFieldValues vs PrivateFieldDescriptors HOT 10
- Using "_" instead of "#" HOT 2
- Update progress of Babel Implementation HOT 3
- Update naming, et. al., to align with latest class fields changes HOT 1
- Writing the same method value to a private method should work HOT 5
- Private function and var naming with # looks so odd, can we use __ instead HOT 3
- Inline ins and del in grammar productions not working HOT 1
- Using # as part of the name of the field is not javascripty HOT 5
- Accessible from a super class? HOT 3
- Should accessing a private name of incorrect type be an early error? HOT 22
- How syntax consensus has been formed over private elements? HOT 1
- Scoped class methods, properties HOT 2
- Please use words that make sense; HOT 3
- Extending a Base Class. Private reference not allowed? HOT 8
- spec: update syntax to forbid private method in object literal HOT 1
- Little suggestion for a better syntax HOT 2
- Should 'obj?.#prop' be allowed? HOT 3
- What does #constructor leave for? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-private-methods.