Defining Getters and Setters

  • Revision slug: Talk:JavaScript/Guide/Obsolete_Pages/Creating_New_Objects/Defining_Getters_and_Setters
  • Revision title: Defining Getters and Setters
  • Revision id: 169074
  • Created:
  • Creator: Harald Albrecht
  • Is current revision? No
  • Comment /* Getter/Setter Definition Clarification */

Revision Content

Getter/Setter Definition Clarification

I would like to suggest to clarify syntactic issues with defining getters and setters. Like:

In principle, getters and setters can be either

  • defined using object initializers, or
  • added later to any object at any time using a getter or setter adding method.

When defining getters and setters using object initializers all you need to do is to prefix a getter method with <tt>get</tt> and a setter method with <tt>set</tt>. Of course, the getter method must not expect a parameter, while the setter method expects exactly one parameter (the new value to set). For instance:

o = {
  a:7,
  get b() { return this.a+1; },
  set c(x) { this.a = x/2; }
};

Getters and setters can also be added to an object at any time after creation using two special methods called <tt>__defineGetter__</tt> and <tt>__defineSetter__</tt>. Both methods expect the name of the getter or setter as their first parameter, in form of a string. The second parameter is the function to call as the getter or setter. For instance (following the previous example):

o.__defineGetter__("b", function() { return this.a+1; });
o.__defineSetter__("c", function(x) { this.a = x/2; });

Which of the two forms to choose depends on your programming style and task at hand. If you already go for the object initializer when defining a prototype you will probably most of the time choose the first form. This form is more compact and natural. However, if you need to add getters and setters later – because you did not wrote the prototype or particular object – then the second form is the only possible form. The second form probably best represents the dynamic nature of JavaScript – but it can make the code hard to read and understand.

Harald Albrecht 08:28, 15 Jun 2005 (PDT)

Revision Source

<h4 name="Getter.2FSetter_Definition_Clarification"> Getter/Setter Definition Clarification </h4>
<p>I would like to suggest to clarify syntactic issues with defining getters and setters. Like:
</p><p>In principle, getters and setters can be either
</p>
<ul><li> defined using <a href="en/Core_JavaScript_1.5_Guide/Creating_New_Objects/Using_Object_Initializers"> object initializers</a>, or
</li><li> added later to any object at any time using a getter or setter adding method.
</li></ul>
<p>When defining getters and setters using <a href="en/Core_JavaScript_1.5_Guide/Creating_New_Objects/Using_Object_Initializers"> object initializers</a> all you need to do is to prefix a getter method with <tt>get</tt> and a setter method with <tt>set</tt>. Of course, the getter method must not expect a parameter, while the setter method expects exactly one parameter (the new value to set). For instance:
</p>
<pre class="eval">o = {
  a:7,
  <b>get</b> b() { return this.a+1; },
  <b>set</b> c(x) { this.a = x/2; }
};
</pre>
<p>Getters and setters can also be added to an object at any time after creation using two special methods called <tt>__defineGetter__</tt> and <tt>__defineSetter__</tt>. Both methods expect the name of the getter or setter as their first parameter, in form of a string. The second parameter is the function to call as the getter or setter. For instance (following the previous example):
</p>
<pre class="eval">o.__defineGetter__("b", function() { return this.a+1; });
o.__defineSetter__("c", function(x) { this.a = x/2; });
</pre>
<p>Which of the two forms to choose depends on your programming style and task at hand. If you already go for the object initializer when defining a prototype you will probably most of the time choose the first form. This form is more compact and natural. However, if you need to add getters and setters later – because you did not wrote the prototype or particular object – then the second form is the only possible form. The second form probably best represents the dynamic nature of JavaScript – but it can make the code hard to read and understand.
</p><p><a href="User:Harald_Albrecht">Harald Albrecht</a> 08:28, 15 Jun 2005 (PDT)
</p>
Revert to this revision