Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Something like properties

Reply
Thread Tools

Something like properties

 
 
Christoph Basedau
Guest
Posts: n/a
 
      04-18-2005
Hi,


function F()
{
var m_flag = 42;
this.Flag = m_flag;
this.SetFlag = function(val){m_flag = val;}
this.GetFlag = function(){return 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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      04-18-2005


Christoph Basedau wrote:


> function F()
> {
> var m_flag = 42;
> this.Flag = m_flag;
> this.SetFlag = function(val){m_flag = val;}
> this.GetFlag = function(){return 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
> ?


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.__defineGetter__ ('flag',
function () {
return this._flag;
}
);
F.prototype.__defineSetter__('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/
 
Reply With Quote
 
 
 
 
Christoph Basedau
Guest
Posts: n/a
 
      04-18-2005
18.04.2005 13:22, Martin Honnen schrieb:
>
> Christoph Basedau wrote:>


[Getter/Setter-Properties in Javascript]

> 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.__defineGetter__ ('flag',
> function () {
> return this._flag;
> }
> );
> F.prototype.__defineSetter__('flag',
> function (flag) {
> return this._flag = flag;
> }
> );
>
> var f = new F('Kibo');
> alert(f.flag);
> alert(f.flag = 'Xibo');


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
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      04-19-2005


Christoph Basedau wrote:

> 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?


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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can I overload operators like "=>", "->" or something like that? dmitrey Python 5 04-20-2012 05:49 PM
XPath query for <?define something="something" ?> Pekka Järvinen XML 2 04-29-2008 08:12 PM
How to find and replace something that is nested inside something else? alainfri@gmail.com Perl Misc 4 05-31-2007 11:50 PM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
umm... something... template(s)... something else... pointer(s)... and such... 0.o yah, I'm hopeless and clueless o.0 C++ 4 10-13-2004 10:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57