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", "Geeksforgeeks"],
get GFG() {
if (this.arr.length === 0) return undefined;
return this.arr[this.arr.length - 1];
}
};
console.log(obj.GFG);
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);
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);
OutputThis 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);
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);
OutputThis 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);
OutputThis is a static getter
Similar Reads
JavaScript Map get() Method The Map.get() method in JavaScript is a convenient way to retrieve the value associated with a specific key in a Map object. A Map in JavaScript allows you to store key-value pairs where keys can be of any data type, making it more useful compared to objects, which only allow strings and symbols as
3 min read
Servlet - Fetching Result Servlet is a simple java program that runs on the server and is capable to handle requests from the client and generate dynamic responses for the client. How to Fetch a Result in Servlet? It is depicted below stepwise as shown below as follows: You can fetch a result of an HTML form inside a Servlet
3 min read
JavaScript Know the value of GET parameters from URL In this article, we will see how to get the GET parameters from a URL using JavaScript. In order to know the parameters that are passed by the "GET" method, sometimes we need to pass the Email and other details. For that purpose, we are using the following snippet of code. Example 1: This example ge
1 min read
How to Call a Method in Java? In Java, calling a method helps us to reuse code and helps everything be organized. Java methods are just a block of code that does a specific task and gives us the result back. In this article, we are going to learn how to call different types of methods in Java with simple examples.What is a Metho
3 min read
Java Program to Get Connected to a Web Server In today's interconnected world, connecting to web servers is a fundamental skill for software developers. Whether you're building a web application that needs to fetch data from external sources, testing a web service, or even performing web scraping for research, knowing how to interact with web s
4 min read