Object.prototype, getters and setters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wei Wang

    #1

    Object.prototype, getters and setters

    Greetings,

    I find the JavaScript's Object.prototyp e and getter/setter mechanism
    very nice. However, I need some help with extending an object with
    getters/setters in the derived class. For example:

    A : function () {}

    A.prototype =
    {
    a : null,

    get a : function () { return a++; }
    };


    B : function () {}

    B.prototype = new A;

    Now, I would like to define a getter/setter in B. How do I do that?
    There is no way to use the same syntax as in "A.prototyp e = ..." above.

    Thanks for your help.

    Regards,
    --
    Wei
  • Martin Honnen

    #2
    Re: Object.prototyp e, getters and setters



    Wei Wang wrote:

    [color=blue]
    > I find the JavaScript's Object.prototyp e and getter/setter mechanism
    > very nice. However, I need some help with extending an object with
    > getters/setters in the derived class. For example:
    >
    > A : function () {}
    >
    > A.prototype =
    > {
    > a : null,
    >
    > get a : function () { return a++; }
    > };
    >
    >
    > B : function () {}
    >
    > B.prototype = new A;
    >
    > Now, I would like to define a getter/setter in B. How do I do that?
    > There is no way to use the same syntax as in "A.prototyp e = ..." above.[/color]

    What you have looks like some pseudo syntax to me, here is how it works
    in pratice:

    function A () {
    this._a = 0;
    }
    A.prototype.__d efineGetter__(' a',
    function () {
    return this._a++;
    }
    );


    function B () {
    A.apply(this);
    this._b = 0;
    }
    B.prototype = new A();
    B.prototype.__d efineGetter__(' b',
    function () {
    return this._b--;
    }
    );

    var b = new B();
    alert(b.a);
    alert(b.a);
    alert(b.b);
    alert(b.b);

    And note that getters/setters are an extension to the ECMAScript edition
    3 standard that is only implemented in the Mozilla JavaScript engine.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Object.prototyp e, getters and setters

      Martin Honnen wrote:
      [color=blue]
      > Wei Wang wrote:[color=green]
      >> [...]
      >> A : function () {}
      >>
      >> A.prototype =
      >> {
      >> a : null,
      >>
      >> get a : function () { return a++; }
      >> };
      >>
      >>
      >> B : function () {}
      >>
      >> B.prototype = new A;
      >>
      >> Now, I would like to define a getter/setter in B. How do I do that?
      >> There is no way to use the same syntax as in "A.prototyp e = ..." above.[/color]
      >
      > What you have looks like some pseudo syntax to me,[/color]

      But it is not entirely. Prototypes of objects inheriting directly
      from Object can be defined using an Object literal and getters may
      be defined with the `get' keyword before the property identifier:

      <http://developer-test.mozilla.or g/docs/Core_JavaScript _1.5_Guide:Crea ting_New_Object s:Defining_Gett ers_and_Setters >

      Whether the latter really *works* in an implementation is another issue.
      It does work in JavaScript 1.5 as implemented in Mozilla/5.0 (tested
      successfully with `Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8)
      Gecko/20050513 Firefox/1.0.4 (Debian package 1.0.4-1) Mnenhy/0.7.2.0'):

      var A = function () {}; // although I'd prefer: function A() {}

      A.prototype = {
      a: null,
      get b() { return this.a++; }
      };

      var x = new A;
      alert(x.b); // 0
      alert(x.b); // 1
      [color=blue]
      > here is how it works in pratice: [...][/color]

      Since the above works in practice as well, I don't see a need to call
      the __defineGetter_ _() method explicitely. Since method calls are
      less efficient than variable instantiation, one could probably increase
      efficiency with my approach.

      What of course does not work is having `a' as identifier for both the
      non-function property and the getter; identifiers have to be unique
      within a scope.


      PointedEars

      Comment

      Working...