473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Something like properties

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
Jul 23 '05 #1
3 1001


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/
Jul 23 '05 #2
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
Jul 23 '05 #3


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/
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: nobody | last post by:
I just tried to do something and realized I'm not sure of the proper (if there *IS* a proper) way to do it. I'm designing a class with a lot a methods/properties. I would like to group some of...
1
by: Stephanie Stowe | last post by:
Hi. I am trying to read information out of the IIS metabase (v5.1). Observe the following code: using System; using System.DirectoryServices; using System.Reflection; namespace ADSI1 {...
5
by: nobody | last post by:
I just tried to do something and realized I'm not sure of the proper (if there *IS* a proper) way to do it. I'm designing a class with a lot a methods/properties. I would like to group some of...
10
by: nobody | last post by:
I just tried to do something and realized I'm not sure of the proper (if there *IS* a proper) way to do it. I'm designing a class with a lot a methods/properties. I would like to group some of...
9
by: pamelafluente | last post by:
Hi, I was "studying" the famous (public code) BusyBox. I see the instruction: var busyBox = new BusyBox as in var busyBox = new BusyBox("BusyBox1", "busyBox", 4, "gears_ani_", ".gif",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.