![]() |
VOOZH | about |
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.
{ get prop() { /* … */ } }
{ get [expression]() { /* … */ } }
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 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
Geeksforgeeks
You can define getters within the classes to access computed properties or provide encapsulated access to private variables.
Example:
1
delete OperatorYou can remove a getter from an object using delete operator.
Example:
This is a getter Function undefined
definePropertyYou can add a getter to an existing object using the Object.defineProperty.
Example:
Dynamic getter
You can define a getter with a computed property name allowing dynamic property access.
Example:
This is computed property name
Static getters are associated with the class rather than instances and can be accessed directly on the class.
Example:
This is a static getter