Há um artigo no MDN Docs sobre Private properties, e eu quero que explique esse artigo:
"Private properties
Private properties are counterparts of the regular class properties which are public, including class fields, class methods, etc. Private properties get created by using a hash # prefix and cannot be legally referenced outside of the class. The privacy encapsulation of these class properties is enforced by JavaScript itself. The only way to access a private property is via dot notation, and you can only do so within the class that defines the private property.
Private properties were not native to the language before this syntax existed. In prototypal inheritance, its behavior may be emulated with WeakMap objects or closures, but they can't compare to the # syntax in terms of ergonomics.
Syntax
js
Copy to Clipboard
class ClassWithPrivate {
#privateField;
#privateFieldWithInitializer = 42;
static #privateStaticMethod() {
// …
}
}
There are some additional syntax restrictions:
All private identifiers declared within a class must be unique. The namespace is shared between static and instance properties. The only exception is when the two declarations define a getter-setter pair.
The private identifier cannot be #constructor.
Description
Most class properties have their private counterparts:
Private fields
Private methods
Private static fields
Private static methods
Private getters
Private setters
Private static getters
Private static setters
These features are collectively called private properties. However, constructors cannot be private in JavaScript. To prevent classes from being constructed outside of the class, you have to use a private flag.
Private properties are declared with # names (pronounced "hash names"), which are identifiers prefixed with #. The hash prefix is an inherent part of the property name — you can draw relationship with the old underscore prefix convention _privateField — but it's not an ordinary string property, so you can't dynamically access it with the bracket notation.
It is a syntax error to refer to # names from outside of the class. It is also a syntax error to refer to private properties that were not declared in the class body, or to attempt to remove declared properties with delete.
js
Copy to Clipboard
class ClassWithPrivateField {
#privateField;
const instance = new ClassWithPrivateField();
instance.#privateField; // Syntax error
JavaScript, being a dynamic language, is able to perform this compile-time check because of the special hash identifier syntax, making it different from normal properties on the syntax level.
Note: Code run in the Chrome console can access private properties outside the class. This is a DevTools-only relaxation of the JavaScript syntax restriction.
Assistant
If you access a private property from an object that doesn't have the property, a TypeError is thrown, instead of returning undefined as normal properties do.
js
Copy to Clipboard
class C {
#x;
static getX(obj) {
return obj.#x;
}
}
console.log(C.getX(new C())); // undefined
console.log(C.getX({})); // TypeError: Cannot read private member #x from an object whose class did not declare it
This example also illustrates that you can access private properties 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 property. This will return true if the private field or method exists, and false otherwise.
js
Copy to Clipboard
class C {
#x;
constructor(x) {
this.#x = x;
}
static getX(obj) {
if (#x in obj) return obj.#x;
code
return "obj must be an instance of C";
}
}
console.log(C.getX(new C("foo"))); // "foo"
console.log(C.getX(new C(0.196))); // 0.196
console.log(C.getX(new C(new Date()))); // the current date and time
console.log(C.getX({})); // "obj must be an instance of C"
Note a corollary of private names being always pre-declared and non-deletable: if you found that an object possesses one private property of the current class (either from a try...catch or an in check), it must possess all other private properties. An object possessing the private properties of a class generally means it was constructed by that class (although not always).
Private properties are not part of the prototypical inheritance model since they can only be accessed within the current class's body and aren't inherited by subclasses. Private properties with the same name within different classes are entirely different and do not interoperate with each other. See them as external metadata attached to each instance, managed by the class. For this reason, structuredClone() does not clone private properties, and Object.freeze() and Object.seal() have no effect on private properties.
For more information on how and when private fields are initialized, see public class fields.
Examples
Private fields
Private fields include private instance fields and private static fields. Private fields are only accessible from inside the class declaration.
Private instance fields
Like their public counterparts, private instance fields:
are added before the constructor runs in a base class, or immediately after super() is invoked in a subclass, and
are only available on instances of the class.
js
Copy to Clipboard
class ClassWithPrivateField {
#privateField;
constructor() {
this.#privateField = 42;
}
}
class Subclass extends ClassWithPrivateField {
#subPrivateField;
new Subclass(); // In some dev tools, it shows Subclass {#privateField: 42, #subPrivateField: 23}
Note: #privateField from the ClassWithPrivateField base class is private to ClassWithPrivateField and is not accessible from the derived Subclass.
Returning overriding object
A class's constructor can return a different object, which will be used as the new this for the derived class constructor. The derived class may then define private fields on that returned object — meaning it is possible to "stamp" private fields onto unrelated objects.
js
Copy to Clipboard
class Stamper extends class {
// A base class whose constructor returns the object it's given
constructor(obj) {
return obj;
}
} {
// This declaration will "stamp" the private field onto the object
// returned by the base class constructor
#stamp = 42;
static getStamp(obj) {
return obj.#stamp;
}
}
const obj = {};
new Stamper(obj);
// Stamper calls Base, which returns obj, so obj is
// now the this value. Stamper then defines #stamp on obj
console.log(obj); // In some dev tools, it shows {#stamp: 42}
console.log(Stamper.getStamp(obj)); // 42
console.log(obj instanceof Stamper); // false
// You cannot stamp private properties twice
new Stamper(obj); // Error: Initializing an object twice is an error with private fields
Warning: This is a potentially very confusing thing to do. You are generally advised to avoid returning anything from the constructor — especially something unrelated to this.
Private static fields
Like their public counterparts, private static fields:
are added to the class constructor at class evaluation time, and
are only available on the class itself.
js
Copy to Clipboard
class ClassWithPrivateStaticField {
static #privateStaticField = 42;
console.log(ClassWithPrivateStaticField.publicStaticMethod()); // 42
There is a restriction on private static fields: only the class which defines the private static field can access the field. This can lead to unexpected behavior when using this. In the following example, this refers to the Subclass class (not the ClassWithPrivateStaticField class) when we try to call Subclass.publicStaticMethod(), and so causes a TypeError.
js
Copy to Clipboard
class ClassWithPrivateStaticField {
static #privateStaticField = 42;
class Subclass extends ClassWithPrivateStaticField {}
Subclass.publicStaticMethod(); // TypeError: Cannot read private member #privateStaticField from an object whose class did not declare it
This is the same if you call the method with super, because super methods are not called with the super class as this.
js
Copy to Clipboard
class ClassWithPrivateStaticField {
static #privateStaticField = 42;
static publicStaticMethod() {
// When invoked through super, this still refers to Subclass
return this.#privateStaticField;
}
}
Subclass.callSuperMethod(); // TypeError: Cannot read private member #privateStaticField from an object whose class did not declare it
You are advised to always access private static fields through the class name, not through this, so inheritance doesn't break the method.
Private methods
Private methods include private instance methods and private static methods. Private methods are only accessible from inside the class declaration.
Private instance methods
Unlike their public counterparts, private instance methods:
are installed immediately before the instance fields are installed, and
are only available on instances of the class, not on its .prototype property.
js
Copy to Clipboard
class ClassWithPrivateMethod {
#privateMethod() {
return 42;
}
const instance = new ClassWithPrivateMethod();
console.log(instance.publicMethod()); // 42
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.
js
Copy to Clipboard
class ClassWithPrivateAccessor {
#message;
get #decoratedMessage() {
return 🎬${this.#message}🛑;
}
set #decoratedMessage(msg) {
this.#message = msg;
}
new ClassWithPrivateAccessor(); // 🎬hello world🛑
Unlike public methods, private methods are not accessible on the .prototype property of their class.
js
Copy to Clipboard
class C {
#method() {}
static getMethod(x) {
return x.#method;
}
}
console.log(C.getMethod(new C())); // [Function: #method]
console.log(C.getMethod(C.prototype)); // TypeError: Receiver must be an instance of class C
Private static methods
Like their public counterparts, private static methods:
are added to the class constructor at class evaluation time, and
are only available on the class itself.
js
Copy to Clipboard
class ClassWithPrivateStaticMethod {
static #privateStaticMethod() {
return 42;
}
console.log(ClassWithPrivateStaticMethod.publicStaticMethod()); // 42
Private static methods may be generator, async, and async generator functions.
The same restriction previously mentioned for private static fields holds for private static methods, and similarly can lead to unexpected behavior when using this. In the following example, when we try to call Subclass.publicStaticMethod(), this refers to the Subclass class (not the ClassWithPrivateStaticMethod class) and so causes a TypeError.
js
Copy to Clipboard
class ClassWithPrivateStaticMethod {
static #privateStaticMethod() {
return 42;
}
class Subclass extends ClassWithPrivateStaticMethod {}
console.log(Subclass.publicStaticMethod()); // TypeError: Cannot read private member #privateStaticMethod from an object whose class did not declare it
Simulating private constructors
Many other languages include the capability to mark a constructor as private, which prevents the class from being instantiated outside of the class itself — you can only use static factory methods that create instances, or not be able to create instances at all. JavaScript does not have a native way to do this, but it can be accomplished by using a private static flag.
js
Copy to Clipboard
class PrivateConstructor {
static #isInternalConstructing = false;
constructor() {
if (!PrivateConstructor.#isInternalConstructing) {
throw new TypeError("PrivateConstructor is not constructable");
}
PrivateConstructor.#isInternalConstructing = false;
// More initialization logic
}
new PrivateConstructor(); // TypeError: PrivateConstructor is not constructable
PrivateConstructor.create(); // PrivateConstructor {}"
Mas, lembre dessas regras:
O que eu gostaria que você soubesse sobre mim para obter respostas melhores:
1 - Não consigo entender palavras difíceis.
2 - Sou iniciante no mundo da tecnologia.
O que eu gostaria que você respondesse:
1 - Você deve sempre ser detalhista em qualquer tipo de informação que eu pedir, independente se possa ser respondido com 1 linha, precisa ser detalhista, mas não podemos utilizar palavras difíceis para não dificultar a leitura.
2 - Você deve ser imparcial em tudo o que disser.
3 - A conversa precisa ser amigável, então não é pra conversar com um tom muito sério.
4 - Não assuma que sei de alguma coisa, pense que se você introduzir algum conceito diferente, é provavel que eu não saiba.
5 - Não elimine informação do artigo na sua resposta, não faça um tipo de resumo, tudo que eles forem apresentar no artigo como informação, deve ser explicado, só que mais fácil.
Claro! Vamos explicar tudo sobre propriedades privadas em JavaScript de uma forma bem simples e detalhada. Não se preocupe se alguns termos parecerem novos para você, vou explicar tudo passo a passo.
O que são Propriedades Privadas?
Em JavaScript, quando trabalhamos com classes (que são como moldes para criar objetos), temos propriedades que podem ser públicas ou privadas.
Propriedades Públicas: São as que qualquer pessoa (ou parte do código) pode ver e modificar.
Propriedades Privadas: São escondidas e só podem ser usadas dentro da própria classe. Ninguém de fora consegue acessar ou mudar essas propriedades diretamente.
Como Criar Propriedades Privadas?
Para criar uma propriedade privada, usamos um símbolo especial chamado hash (#) antes do nome da propriedade. Veja um exemplo:
javascript
class MinhaClasse {
#minhaPropriedadePrivada;
constructor() {
this.#minhaPropriedadePrivada = 42;
}
meuMetodo() {
return this.#minhaPropriedadePrivada;
}
}
const instancia = new MinhaClasse();
console.log(instancia.meuMetodo()); // 42
console.log(instancia.#minhaPropriedadePrivada); // Isso dá erro!
No exemplo acima:
#minhaPropriedadePrivada é uma propriedade privada.
Dentro da classe (MinhaClasse), podemos acessar essa propriedade usando this.#minhaPropriedadePrivada.
Fora da classe, tentar acessar instancia.#minhaPropriedadePrivada dá um erro. Isso significa que ninguém pode mexer nessa propriedade diretamente.
Regras Importantes das Propriedades Privadas
Nomes Únicos: Cada propriedade privada dentro de uma classe deve ter um nome único. Isso evita confusão entre diferentes propriedades privadas.
Não Pode Ser #constructor: Você não pode chamar uma propriedade privada de #constructor. Esse nome já é reservado para a função que cria a instância da classe.
Acessar Somente de Dentro: Só dá para usar propriedades privadas dentro da própria classe. Fora dela, o JavaScript não permite acessar essas propriedades.
Erro de Sintaxe: Se tentar acessar uma propriedade privada que não existe ou que está fora da classe, o JavaScript mostrará um erro.
Tipos de Propriedades Privadas
As propriedades privadas podem ser de vários tipos:
Campos Privados: São variáveis privadas dentro da classe.
Métodos Privados: Funções privadas que só a classe pode usar.
Campos e Métodos Estáticos Privados: São propriedades e métodos que pertencem à própria classe, e não às instâncias (objetos) criadas a partir dela.
São funções privadas que pertencem à própria classe.
javascript
class Util {
static #mensagemPrivada() {
return "Esta é uma mensagem secreta!";
}
static exibirMensagem() {
return Util.#mensagemPrivada();
}
}
console.log(Util.exibirMensagem()); // "Esta é uma mensagem secreta!"
console.log(Util.#mensagemPrivada()); // Erro!
Restrições e Cuidados
Herança e Subclasses: Se você tem uma classe que herda de outra (subclasse), as propriedades privadas da classe pai não são acessíveis na subclasse.
Clone e Congelar Objetos: Ferramentas como structuredClone() não conseguem copiar propriedades privadas. Da mesma forma, métodos como Object.freeze() e Object.seal() não afetam as propriedades privadas.
Acesso Estático com this: Quando usamos propriedades estáticas privadas com this, pode causar erros na herança. É melhor acessar propriedades privadas estáticas diretamente pelo nome da classe para evitar problemas.
Simulando Construtores Privados
JavaScript não permite criar construtores privados diretamente, mas podemos simular isso usando uma bandeira (flag) privada. Isso impede que alguém crie uma nova instância da classe fora de métodos controlados.
javascript
class ConstrutorPrivado {
static #interno = false;
constructor() {
if (!ConstrutorPrivado.#interno) {
throw new TypeError("ConstrutorPrivado não pode ser instanciado diretamente!");
}
ConstrutorPrivado.#interno = false;
// Mais lógica de inicialização
}
static criarInstancia() {
ConstrutorPrivado.#interno = true;
return new ConstrutorPrivado();
}
}
const instancia = ConstrutorPrivado.criarInstancia(); // Funciona
const outraInstancia = new ConstrutorPrivado(); // Erro!
Nesse exemplo:
A propriedade privada #interno é usada para controlar se a classe pode ser instanciada.
Apenas o método estático criarInstancia consegue criar uma nova instância.
Tentar criar uma instância diretamente usando new ConstrutorPrivado() resulta em um erro.
Resumo
Propriedades Privadas: São acessíveis somente dentro da própria classe, usando o símbolo #.
Campos e Métodos: Podem ser privados ou públicos, e também podem ser estáticos (pertencentes à classe) ou de instância (pertencentes aos objetos).
Regras de Acesso: Fora da classe, tentar acessar propriedades privadas dá erro. Dentro da classe, usamos this.#propriedade para acessar.
Cuidados: Propriedades privadas não são herdadas e não podem ser copiadas ou congeladas usando métodos comuns de manipulação de objetos.
Espero que essa explicação tenha ajudado a entender melhor como funcionam as propriedades privadas em JavaScript! Se tiver mais dúvidas, fique à vontade para perguntar. 😊