A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript-object-__definegetter__-method/ below:

JavaScript Object __defineGetter__() Method - GeeksforGeeks

JavaScript Object __defineGetter__() Method

Last Updated : 15 Jul, 2025

The __defineGetter__() method is used to bind an object's property to a function which will be called when the specified property is looked up. It is recommended to use the object initializer syntax or the Object.defineProperty() API instead of this method as it is being deprecated.

Syntax: 

obj.__defineGetter__( prop, func )

Parameters: This function accepts two parameters as given above and described below:

Return Values: This method returns undefined.

Example 1: Using the __defineGetter__ () method

JavaScript
let obj = {};
obj.__defineGetter__('printTen', function () {
    return 10;
});
console.log(obj.printTen);

 Output:

10

Example 2: Using the standard-compliant way using object initializer syntax and Object.defineProperty() API. 

JavaScript
// Using the get operator
let obj = {
    get printTen() { return 10; }
};
console.log(obj.printTen);

// Using Object.defineProperty
let obj1 = {};
Object.defineProperty(obj1, 'printTwo', {
    get: function () {
        return 2;
    }
});
console.log(obj1.printTwo);

 Output:

10
2

We have a complete list of Javascript Object Methods, to check those please go through the Javascript Object Complete Reference article.

Supported Browser:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4