๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Classes โ€บ Private_elements
Private elements - JavaScript - MDN Web Docs
July 8, 2025 - This example also illustrates that you can access private elements within static functions too, and on externally defined instances of the class. You can use the in operator to check whether an externally defined object possesses a private element. This will return true if the private field ...
๐ŸŒ
Medium
medium.com โ€บ web-factory-llc โ€บ private-fields-private-methods-private-accessors-javascript-finally-with-privacy-classes-4a476d9cd679
Private fields, private methods, private accessors. Javascript finally with privacy classes
July 23, 2020 - Defining private fields in class (and using them in object โ€” instance for that class) means accessing that field will be allowed only to the current object (inside that same class).
Discussions

javascript - How to access a private field in a class - Stack Overflow
I have a Movie class and two fields with underscores. I need to create a method that returns the title of the movie. How can i do this? Now in the console the error movieName is not defined ... Neither of these is private. They just have an underscore at the beginning. You access them the same way ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
class - Private properties in JavaScript ES6 classes - Stack Overflow
@aij So name one language that does not do the same. You can easily see that he could just comment out the setter or the getter or both and that _name is truly private. 2019-01-15T22:57:36.72Z+00:00 ... Yes, prefix the name with # and include it in the class definition, not just the constructor. ... Real private properties were finally added in ES2022. As of 2023-01-01, private properties (fields ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Access private variables from different instance of object but same class
You can use reflection . However be sure that this is really needed. What exactly are you trying to do? More on reddit.com
๐ŸŒ r/javahelp
8
1
April 1, 2021
Why are inherited private class fields not accessible on the subclass, after instantiation? +are there any workarounds?
Because that's what private means. JS doesn't have a protected field type, which is what you're after. More on reddit.com
๐ŸŒ r/learnjavascript
36
5
December 6, 2025
๐ŸŒ
GitHub
github.com โ€บ tc39 โ€บ proposal-class-fields โ€บ issues โ€บ 257
How to dynamically access private properties/classes ยท Issue #257 ยท tc39/proposal-class-fields
July 18, 2019 - In traditional Javascript objects, and public properties of classes, I can dynamically access properties using array notation, but not private properties using similar notation: class Foo { bam = 'boom'; #bar = 'baz'; constructor(p1, p2)...
Author ย  d42ohpaz
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ es6 โ€บ javascriptโ€™s new private class fields, and how to use them
JavaScript's New Private Class Fields, and How to Use Them โ€” SitePoint
November 13, 2024 - In JavaScript, private class fields are declared by prefixing the field name with a hash (#) symbol. For example, to declare a private field named โ€˜valueโ€™ within a class, you would write #value.
๐ŸŒ
MDN
mdn2.netlify.app โ€บ en-us โ€บ docs โ€บ web โ€บ javascript โ€บ reference โ€บ classes โ€บ private_class_fields
Private class features - JavaScript | MDN
Private instance methods are methods available on class instances whose access is restricted in the same manner as private instance fields. class ClassWithPrivateMethod { #privateMethod() { return 'hello world'; } getPrivateMessage() { return ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Classes โ€บ Public_class_fields
Public class fields - JavaScript - MDN Web Docs - Mozilla
All instance and static methods are added beforehand and can be accessed, although calling them may not behave as expected if they refer to fields below the one being initialized. ... class C { a = 1; b = this.c; c = this.a + 1; d = this.c + 1; } const instance = new C(); console.log(instance.d); // 3 console.log(instance.b); // undefined ยท Note: This is more important with private fields, because accessing a non-initialized private field throws a TypeError, even if the private field is declared below.
๐ŸŒ
DEV Community
dev.to โ€บ smitterhane โ€บ private-class-fields-in-javascript-es2022-3b8
Private class fields in Javascript (ES2022) - DEV Community
July 17, 2022 - ... As of ES2022, you can define private fields for a class using # sign prefixed to the identifiers. You can access private fields from the class constructor within the class declaration.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Errors โ€บ Get_set_missing_private
TypeError: can't access/set private field or method: object is not the right class - JavaScript | MDN
January 25, 2026 - To fix this, either change the field to be an instance field, or access the field on the class itself, or declare another field on the instance. Note that the private namespace is shared between static and instance properties, so you cannot have a static and instance private element with the ...
Find elsewhere
๐ŸŒ
W3cubDocs
docs.w3cub.com โ€บ javascript โ€บ classes โ€บ private_class_fields
Private Class Fields - JavaScript - W3cubDocs
August 8, 2022 - Private instance methods are methods available on class instances whose access is restricted in the same manner as private instance fields. class ClassWithPrivateMethod { #privateMethod() { return "hello world"; } getPrivateMessage() { return this.#privateMethod(); } } const instance = new ClassWithPrivateMethod(); console.log(instance.getPrivateMessage()); // hello world ยท Private instance methods may be generator, async, or async generator functions. Private getters and setters are also possible, and follow the same syntax requirements as their public getter and setter counterparts.
๐ŸŒ
Jamie
jamie.build โ€บ javascripts-new-private-class-fields.html
JavaScript's new #private class fields โ€“ @thejameskyle
Because of #encapsulation (see the "Encapsulation" section above), we need to allow public and private fields with the same name at the same time. So accessing a private field can't just be a normal lookup. Public fields in JavaScript can be referenced via this.field or this['field']. Private ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ why are inherited private class fields not accessible on the subclass, after instantiation? +are there any workarounds?
r/learnjavascript on Reddit: Why are inherited private class fields not accessible on the subclass, after instantiation? +are there any workarounds?
December 6, 2025 -

tldr: i found a method to pass values to private properties declared "further up the chain".. in a subclass definition. i was pleased with this, very pleased, but then i realized that afterwards, even while using getters/setters the private properties are inaccessible on the object, despite the JavaScript debug console showing them on them.

i know there is high strangeness around private properties. But it would mean the world to me, if i could just access them.. somehow.

Top answer
1 of 16
206

No, there's no way to do it. That would essentially be scoping in reverse.

Methods defined inside the constructor have access to private variables because all functions have access to the scope in which they were defined.

Methods defined on a prototype are not defined within the scope of the constructor, and will not have access to the constructor's local variables.

You can still have private variables, but if you want methods defined on the prototype to have access to them, you should define getters and setters on the this object, which the prototype methods (along with everything else) will have access to. For example:

function Person(name, secret) {
    // public
    this.name = name;

    // private
    var secret = secret;

    // public methods have access to private members
    this.setSecret = function(s) {
        secret = s;
    }

    this.getSecret = function() {
        return secret;
    }
}

// Must use getters/setters 
Person.prototype.spillSecret = function() { alert(this.getSecret()); };
2 of 16
64

Update: With ES6, there is a better way:

Long story short, you can use the new Symbol to create private fields.
Here's a great description: https://curiosity-driven.org/private-properties-in-javascript

Example:

var Person = (function() {
    // Only Person can access nameSymbol
    var nameSymbol = Symbol('name');

    function Person(name) {
        this[nameSymbol] = name;
    }

    Person.prototype.getName = function() {
        return this[nameSymbol];
    };

    return Person;
}());

For all modern browsers with ES5:

You can use just Closures

The simplest way to construct objects is to avoid prototypal inheritance altogether. Just define the private variables and public functions within the closure, and all public methods will have private access to the variables.

Or you can use just Prototypes

In JavaScript, prototypal inheritance is primarily an optimization. It allows multiple instances to share prototype methods, rather than each instance having its own methods.
The drawback is that this is the only thing that's different each time a prototypal function is called.
Therefore, any private fields must be accessible through this, which means they're going to be public. So we just stick to naming conventions for _private fields.

Don't bother mixing Closures with Prototypes

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

When you use a closure to access a private variable, prototype methods cannot access the variable. So, you have to expose the closure onto this, which means that you're exposing it publicly one way or another. There's very little to gain with this approach.

Which do I choose?

For really simple objects, just use a plain object with closures.

If you need prototypal inheritance -- for inheritance, performance, etc. -- then stick with the "_private" naming convention, and don't bother with closures.

I don't understand why JS developers try SO hard to make fields truly private.

๐ŸŒ
web.dev
web.dev โ€บ learn โ€บ javascript โ€บ classes โ€บ class-fields
Class fields and methods | web.dev
Private fields can't be accessed from elsewhere in a script. This prevents data properties from being altered outside of the getter and setter methods provided to interact with the values they contain, and it prevents direct access to methods ...
๐ŸŒ
Medium
medium.com โ€บ javascript-in-plain-english โ€บ private-members-in-javascript-classes-cb04944f1ae2
How to create private fields and functions in a JavaScript class | by Keerti Kotaru | JavaScript in Plain English
June 23, 2020 - With a new proposal currently in stage-3, a class can have private members, which are not accessible outside the scope of the class. We can create private fields, getters & setters and functions in JavaScript by prefixing it with #. They are ...
๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ classes
Private and protected properties and methods
June 18, 2021 - We canโ€™t access it from outside or from inheriting classes. Private fields do not conflict with public ones. We can have both private #waterAmount and public waterAmount fields at the same time.
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Using_classes
Using classes - JavaScript - MDN Web Docs - Mozilla
January 23, 2026 - However, if anotherColor is not a Color instance, #values won't exist. (Even if another class has an identically named #values private field, it's not referring to the same thing and cannot be accessed here.) Accessing a nonexistent private element throws an error instead of returning undefined like normal properties do.