Something like properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christoph Basedau

    #1

    Something like properties

    Hi,


    function F()
    {
    var m_flag = 42;
    this.Flag = m_flag;
    this.SetFlag = function(val){m _flag = val;}
    this.GetFlag = function(){retu rn m_flag;}
    }

    var f = new F();
    alert(f.Flag + ' ' + f.GetFlag());
    f.SetFlag(-13);
    alert(f.Flag + ' ' + f.GetFlag());


    Can one somehow let 'Flag' always return the *current* value
    of m_flag *without* out using a function?
    Somethin like a property Get/Put in VB/COM so you can write
    f.Flag = -13;
    var myFlag = f.Flag; // return -13
    ?

    --
    Gruesse, Christoph

    Rio Riay Riayo - Gordon Sumner, 1979
  • Martin Honnen

    #2
    Re: Something like properties



    Christoph Basedau wrote:

    [color=blue]
    > function F()
    > {
    > var m_flag = 42;
    > this.Flag = m_flag;
    > this.SetFlag = function(val){m _flag = val;}
    > this.GetFlag = function(){retu rn m_flag;}
    > }
    >
    > var f = new F();
    > alert(f.Flag + ' ' + f.GetFlag());
    > f.SetFlag(-13);
    > alert(f.Flag + ' ' + f.GetFlag());
    >
    >
    > Can one somehow let 'Flag' always return the *current* value
    > of m_flag *without* out using a function?
    > Somethin like a property Get/Put in VB/COM so you can write
    > f.Flag = -13;
    > var myFlag = f.Flag; // return -13
    > ?[/color]

    ECMAScript edition 3 does not know properties with getters/setters but
    JavaScript 1.5 (in Spidermonkey, the engine used in Mozilla browsers)
    implements them as follows as an extension to the standard:

    function F (flag) {
    this.flag = flag;
    }
    F.prototype.__d efineGetter__ ('flag',
    function () {
    return this._flag;
    }
    );
    F.prototype.__d efineSetter__(' flag',
    function (flag) {
    return this._flag = flag;
    }
    );

    var f = new F('Kibo');
    alert(f.flag);
    alert(f.flag = 'Xibo');


    --

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

    Comment

    • Christoph Basedau

      #3
      Re: Something like properties

      18.04.2005 13:22, Martin Honnen schrieb:[color=blue]
      >
      > Christoph Basedau wrote:>[/color]

      [Getter/Setter-Properties in Javascript]
      [color=blue]
      > ECMAScript edition 3 does not know properties with getters/setters but
      > JavaScript 1.5 (in Spidermonkey, the engine used in Mozilla browsers)
      > implements them as follows as an extension to the standard:
      >
      > function F (flag) {
      > this.flag = flag;
      > }
      > F.prototype.__d efineGetter__ ('flag',
      > function () {
      > return this._flag;
      > }
      > );
      > F.prototype.__d efineSetter__(' flag',
      > function (flag) {
      > return this._flag = flag;
      > }
      > );
      >
      > var f = new F('Kibo');
      > alert(f.flag);
      > alert(f.flag = 'Xibo');[/color]

      Thanks a lot Martin,

      Spidermonkey properties work fine. Is JS 1.5, and especially this part,
      going to be standardized in the near future, or will it likely remain a
      Mozilla extension?



      --
      Gruesse, Christoph

      Rio Riay Riayo - Gordon Sumner, 1979

      Comment

      • Martin Honnen

        #4
        Re: Something like properties



        Christoph Basedau wrote:
        [color=blue]
        > Spidermonkey properties work fine. Is JS 1.5, and especially this part,
        > going to be standardized in the near future, or will it likely remain a
        > Mozilla extension?[/color]

        JavaScript 1.5 implements the ECMAScript edition 3 standard but
        getters/setters are an extension which is intentionally implemented in
        this __defineGetter_ _/__defineSetter_ _ way to not conflict with any
        syntax changes future ECMAScript editions might specify.
        As for future ECMAScript editions I am not sure anything is going to
        happen in this regard, there was work on an ECMAScript edition 4 in the
        past with the main feature to introduce a class construct but no
        standard resulted from that, instead Microsoft went ahead with
        JScript.NET and Macromedia with ActionScript supporting classes.

        --

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

        Comment

        Working...