Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript get Function

JavaScript get Function

JavaScript get function is used to access the properties of an object using dot notation or square brackets. It allows you to retrieve the value associated with a particular property key and the get function is often used when working with objects that implement JavaScript’s getter function.

The get syntax mainly binds an object property to a function.

Syntax:

{ get prop() { /* … */ } }
{ get [expression]() { /* … */ } }

 

Parameters:

  • prop: Property name to which we bind the given function.
  • expression: We can also use the expression in place of the property name to bind the given function.

Return Value: The get function() returns the value associated with the specified propertyName in the objectName. If the property does not exist. It will return undefined.

Define Getter on New Objects in Object Initializers

Define a getter function directly in an object initializer using get keyword followed by property name.

Example: In this example, we will create pseudo-property GFG() which will return

Javascript




const obj = {
    arr: ["Geeks", "Geeksforneveropen"],
    get GFG() {
        if (this.arr.length === 0) return undefined;
        return this.arr[this.arr.length - 1];
    }
};
console.log(obj.GFG);


Output

Geeksforneveropen

Using Getters in Classes

You can define getters within the classes to access computed properties or provide encapsulated access to private variables.

Example:

Javascript




class GFG {
    constructor() {
        this._count = 1;
    }
    get count() {
        return this._count;
    }
}
const obj = new GFG();
console.log(obj.count);


Output

1

Deleting a Getter using the delete Operator

You can remove a getter from an object using delete operator.

Example:

Javascript




const obj = {
    get GFG() {
        return "This is a getter Function";
    }
};
console.log(obj.GFG);
delete obj.GFG;
console.log(obj.GFG);


Output

This is a getter Function
undefined

Defining a Getter on Existing Objects using defineProperty

You can add a getter to an existing object using the Object.defineProperty.

Example:

Javascript




const obj = {};
Object.defineProperty(obj, "GFG", {
    get: function () {
        return "Dynamic getter";
    }
});
console.log(obj.GFG);


Output

Dynamic getter

Using a Computed Property Name

You can define a getter with a computed property name allowing dynamic property access.

Example:

Javascript




const prop = "GFG";
const obj = {
    get [prop]() {
        return "This is computed property name ";
    }
};
console.log(obj.GFG);


Output

This is computed property name 

Defining Static Getter

Static getters are associated with the class rather than instances and can be accessed directly on the class.

Example:

Javascript




class GFG {
    static get Property() {
        return "This is a static getter";
    }
}
console.log(GFG.Property);


Output

This is a static getter
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments