Private & Static
Private Properties
In the previous lectures, we've been creating classes and instances, but we've left some of our properties open to direct manipulation. For example, in the User class, we have a password property:
class User {
constructor(name, email) {
this.name = name;
this.email = email;
this.password = null;
}
setPassword(newPassword) {
this.password = newPassword;
}
validatePassword(passwordToCheck) {
if (!this.password) {
console.log('No password set.');
return false;
}
if (passwordToCheck === this.password) {
console.log('It matches!');
return true;
}
console.log('Wrong password!');
return false;
}
}
const ben = new User('ben', 'ben@mail.com');
ben.validatePassword('1234'); // No password set.
ben.setPassword('1234');
ben.validatePassword('1234'); // It Matches!
ben.password = '1212';// what will this do?
ben.validatePassword('1234'); // is this true?
ben.validatePassword('1212');// what about this one?Ideally, we wouldn't want the password property to be directly accessible and modifiable. This is where the concept of private properties comes in.
Naming Convention
JavaScript, prior to ES6, didn't have built-in support for private properties. Developers often used a naming convention to indicate that a property should be treated as private. Conventionally, a property that's intended to be private is prefixed with an underscore _. This serves as a signal to other developers that the property shouldn't be accessed directly.
Here's an example using the _ convention:
While this convention is widely used, it's important to note that it doesn't provide true encapsulation or prevent access to the property. It's more of a suggestion to other developers.
Using # Notation
# NotationStarting from ECMAScript 2019, JavaScript introduced a # notation for denoting private fields directly within the class body. This provides a cleaner and more explicit way to declare private properties. In the User class example, the #password is used as a private property.
Static Properties and Methods
So far, all the methods and properties we've defined in our classes are instance methods and properties. This means they belong to and operate on instances of the class. However, there are cases where it's more appropriate to define methods/properties that are associated with the class itself rather than its instances. These are called static methods/properties.
Static methods are defined using the static keyword before the method/property name. They are referenced by the class itself, not on instances of the class.
For example, if we had a Circle class with a Circle.PI property defined on the class itself:
While each Circle instance could have their own value of PI, it wouldn't make much sense since the value of PI is constant and the same for all circles.
And here is an example of a class with a private static property and a static method:
Quiz!
Consider the following code snippet:
What do you think will log to the console?
What is the purpose of using the # notation for a property in a class?
How do you call a static method on a class?
Challenge: Car Class
Lets create a class called Car that has the following:
Utilize a private property #licensePlate for encapsulation.
Include public properties:
make,model, andyear.Implement an instance method
displayInfo()that logs information about the car.Implement another instance method
honkHorn()to simulate honking the car's horn in the console.Use a static method called
generateLicensePlate()that a license plate'ABC123'.
Summary
In this lecture, we covered the concept of private properties in JavaScript classes using naming conventions and the # notation. We also explored static methods and their use cases, creating a makeAdmin static method for the User class. Static methods provide a way to define utility functions associated with a class itself, rather than with instances of the class.
Last updated