Saturday, September 6, 2025
HomeLanguagesJavascriptJavaScript Object __defineGetter__() Method

JavaScript Object __defineGetter__() Method

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:

  • prop: It is a string that contains the name of the property to bind to the given function.
  • fun: It is a function to be called when the property is looked up.

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:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 11 and above
  • Opera 9.5 and above
  • Safari 3 and above
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

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS