Skip to content
the poor coder

different getters js

Poor Man
Published
Reading time
2 min read
On this page

Different Getters in JavaScript#

Getters are functions that are used to access the properties of an object. In JavaScript, there are different types of getters that can be used depending on the requirement of the code. In this article, we will discuss some of the different getters in JavaScript.

1. Basic Getter#

The basic getter is used to access a property of an object using a function. The syntax for creating a basic getter is as follows:

Text

    let obj = {
        get prop() {
            return 'value';
        }
    };

The above code creates an object with a basic getter for the property "prop". When "prop" is accessed, the getter function will be executed and it will return the value "value".

2. Computed Getter#

A computed getter is used when the property name is not known beforehand and needs to be computed at runtime. The syntax for creating a computed getter is as follows:

Text

    let obj = {
        get [propertyName]() {
            return 'value';
        }
    };

The above code creates an object with a computed getter for the property "propertyName". When "propertyName" is accessed, the getter function will be executed and it will return the value "value".

3. Arrow Function Getter#

An arrow function getter is used when you want to use an arrow function as a getter. The syntax for creating an arrow function getter is as follows:

Text

    let obj = {
        get prop() => 'value'
    };

The above code creates an object with an arrow function getter for the property "prop". When "prop" is accessed, the arrow function will be executed and it will return the value "value".

4. Getter with Parameters#

A getter with parameters is used when you want to pass parameters to the getter function. The syntax for creating a getter with parameters is as follows:

Text

    let obj = {
        get prop(param) {
            return `value ${param}`;
        }
    };

The above code creates an object with a getter for the property "prop" that takes a parameter "param". When "prop" is accessed, the getter function will be executed with the parameter value and it will return the value "value param".

Conclusion#

These are some of the different getters that can be used in JavaScript. Each type of getter has its own use case and can be used depending on the requirement of the code.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC