Object.prototype.__defineGetter__()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/defineGetter
  • Revision title: Object.prototype.__defineGetter__()
  • Revision id: 493577
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment

Revision Content

{{JSRef("Global_Objects", "Object")}} {{ Non-standard_header }} {{ Deprecated_header }}

Summary

The __defineGetter__ method binds an object's property to a function to be called when that property is looked up.

Syntax

obj.__defineGetter__(sprop, fun)

Parameters

sprop
A string containing the name of the property to bind to the given function
fun
A function to be bound to a lookup of the specified property

Description

The __defineGetter__ allows a {{jsxref("Operators/get", "getter")}} to be defined on a pre-existing object.

Examples

// Non-standard and deprecated way

var o = {};
o.__defineGetter__("gimmeFive", function() { return 5; });
console.log(o.gimmeFive); // 5


// Standard-compliant ways

// Using the get operator
var o = { get gimmeFive() {return 5}};
console.log(o.gimmeFive); // 5


// Using Object.defineProperty
var o = {}
Object.defineProperty(o, 'gimmeFive', {
    get: function() {
        return 5;
    }
});
console.log(o.gimmeFive); // 5

Specifications

Not part of any specifications.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} IE 11 {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

Revision Source

<div>
 {{JSRef("Global_Objects", "Object")}} {{ Non-standard_header }} {{ Deprecated_header }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <strong><code>__defineGetter__</code></strong> method binds an object's property to a function to be called when that property is looked up.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>obj</em>.__defineGetter__(<em>sprop</em>, <em>fun</em>)</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>sprop</code></dt>
 <dd>
  A string containing the name of the property to bind to the given function</dd>
 <dt>
  <code>fun</code></dt>
 <dd>
  A function to be bound to a lookup of the specified property</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>The <code>__defineGetter__</code> allows a {{jsxref("Operators/get", "getter")}} to be defined on a pre-existing object.</p>
<h2 id="See_also" name="See_also">Examples</h2>
<pre class="brush: js">
// Non-standard and deprecated way

var o = {};
o.__defineGetter__("gimmeFive", function() { return 5; });
console.log(o.gimmeFive); // 5


// Standard-compliant ways

// Using the get operator
var o = { get gimmeFive() {return 5}};
console.log(o.gimmeFive); // 5


// Using Object.defineProperty
var o = {}
Object.defineProperty(o, 'gimmeFive', {
    get: function() {
        return 5;
    }
});
console.log(o.gimmeFive); // 5</pre>
<h2 id="See_also" name="See_also">Specifications</h2>
<p>Not part of any specifications.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>IE 11</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li>{{jsxref("Operators/get", "get")}} operator</li>
 <li>{{jsxref("Object.defineProperty()")}}</li>
 <li>{{jsxref("Object.lookupGetter", "Object.prototype.__lookupGetter__()")}}</li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Defining_getters_and_setters" title="JavaScript/Guide/Creating_New_Objects/Defining_Getters_and_Setters">JS Guide: Defining Getters and Setters</a></li>
 <li><a href="http://whereswalden.com/2010/04/16/more-spidermonkey-changes-ancient-esoteric-very-rarely-used-syntax-for-creating-getters-and-setters-is-being-removed/">[Blog Post] Deprecation of __defineGetter__ and __defineSetter__</a></li>
</ul>
Revert to this revision