es6 class property

What are ES6 Class Properties?

If you are a JavaScript developer, you might be familiar with classes in JavaScript. ES6 introduced a new syntax for creating classes in JavaScript. One of the features introduced in ES6 classes is class properties.

Class properties are a way to declare properties directly on a class, rather than inside a constructor function. This makes it easier to define properties for instances of the class without having to repeat code in the constructor.

How to Define Class Properties

The syntax for defining class properties is as follows:


class MyClass {
  myProperty = defaultValue;
}

Here, myProperty is a class property and defaultValue is the default value of the property. When an instance of the class is created, it will have this property with the specified default value.

Example Usage

Let's see an example of how class properties can be used:


class Person {
  name = 'John Doe';
  age = 30;

  greet() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}

const person = new Person();
person.greet(); // Output: Hello, my name is John Doe and I am 30 years old.

In the above example, we have defined a class Person with two class properties - name and age. These properties have default values of 'John Doe' and 30 respectively. We have also defined a method greet() which uses these properties to log a greeting message to the console.

We then create a new instance of the Person class and call the greet() method, which logs the expected greeting message.

Other Ways to Define Class Properties

There are other ways to define class properties in JavaScript, such as using getters and setters or defining properties inside the constructor function. However, using class properties is a more concise and readable way to define properties for classes in JavaScript.

Here is an example of defining a class property using a getter:


class MyClass {
  get myProperty() {
    return this._myProperty;
  }
  set myProperty(value) {
    this._myProperty = value;
  }
}

const myInstance = new MyClass();
myInstance.myProperty = 'Hello, World!';
console.log(myInstance.myProperty); // Output: Hello, World!

In the above example, we have defined a class property myProperty using a getter and a setter. The getter returns the value of a private property _myProperty and the setter sets its value. We then create a new instance of the MyClass and set and get its myProperty property using the setter and getter methods respectively.

Conclusion

In conclusion, class properties are a powerful feature of ES6 classes in JavaScript. They allow you to define properties directly on a class, making it easier to define properties for instances of the class without having to repeat code in the constructor. There are other ways to define properties for classes in JavaScript, but using class properties is a more concise and readable way to do it.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe