Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Static class variable creation?

4 views
Skip to first unread message

Bela

unread,
Apr 22, 2003, 8:48:21 AM4/22/03
to
Hi All,

Please help me, How can I create a static class variable in the
JavaScript, maybe with prototype?

Thanks: R. Bela


Martin Honnen

unread,
Apr 22, 2003, 9:46:48 AM4/22/03
to
Bela wrote:
> Hi All,
>
> Please help me, How can I create a static class variable in the
> JavaScript, maybe with prototype?

function YourClass () {
//...
}
YourClass.prototype.classVariable = 'Kibology';

You can then access
this.classVariable
inside methods or
object.classVariable

Richard Cornford

unread,
Apr 22, 2003, 10:07:58 AM4/22/03
to
Bela wrote in message ...

> Please help me, How can I create a static class variable
> in the JavaScript, maybe with prototype?


JavaScript can make a reasonably close approximation of Java static
class variables by defining the variables as properties of the
JavaScript Object constructor function.

So a constructor:-

function myObject(){
//constructor function body
}

- could have the class properties:-

myObject.aString = 'a string';
myObject.aNumber = 4;
myObject.aFunction = function(){
//static function body!
//Note: this function has now knowledge of any
//instances of myObject and 'this' within this function
//is a reference to the global object.
};

- Any code could then read the primitive static variables as:-

var st = myObject.aString;
var nm = myObject.aNumber;

- and call the function as:-

myObject.aFunction();

- HTH

Richard.

Richard Cornford

unread,
Apr 22, 2003, 10:35:04 AM4/22/03
to
Bela wrote in message ...
> Please help me, How can I create a static class variable
> in the JavaScript, maybe with prototype?

Roboz Bela

unread,
Apr 22, 2003, 10:59:59 AM4/22/03
to
Dear Richard,
The static class fields are inaccessible from an instance of the myObject
class.
Moreover, if you don't use the "this" keyword in the class constructor, the
variables will be bound with the window object instead of the myObject
class?
What is your opinion?
R. Bela


"Richard Cornford" <Ric...@litotes.demon.co.uk> az alábbiakat írta a
következő hírüzenetben: b83ih8$sq8$1$8302...@news.demon.co.uk...

Richard Cornford

unread,
Apr 22, 2003, 10:59:30 AM4/22/03
to
Richard Cornford wrote in message ...
<snip>

>myObject.aFunction = function(){
> //static function body!
> //Note: this function has now knowledge of any
> //instances of myObject and 'this' within this function
> //is a reference to the global object.
>};
<snip>

I typoed that comment, which has left it a bit ambiguous. It should have
read:-

myObject.aFunction = function(){
//static function body!

//Note: this function has no knowledge of any


//instances of myObject and 'this' within this function
//is a reference to the global object.
};

Richard.

Douglas Crockford

unread,
Apr 22, 2003, 11:47:31 AM4/22/03
to
> Please help me, How can I create a static class variable in the
> JavaScript, maybe with prototype?

A class will have a constructor function.

function MyClass() {
....
}

You use the prototype for public instance methods. Static members, on the other
hand, are added directly to the constructor. (In JavaScript, functions are
objects.)

MyClass.myConst = 'value';

http://www.crockford.com/javascript/inheritance.html

Laurent Bugnion, GalaSoft

unread,
Apr 22, 2003, 11:51:43 AM4/22/03
to
Hi,

Note however that "static" members created this way are always public.
There is, AFAIK, no way to create private static members in JavaScript.

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Richard Cornford

unread,
Apr 22, 2003, 12:10:09 PM4/22/03
to
Roboz Bela wrote in message ...

>The static class fields are inaccessible from an instance of
>the myObject class.

Properties of the constructor function can be accessed by instances of
the myObject in exactly the same way as they can be accessed from any
other code. Instance methods could call the example function as:-

myObject.aFunction();

>Moreover, if you don't use the "this" keyword in the class
>constructor, the variables will be bound with the window object
>instead of the myObject class?
>What is your opinion?


I don't see the use of the 'this' keyword within the constructor as
relevant. The properties are properties of a function object (a global
function that is itself a property of the global window object),
JavaScript would not care whether that function is actually a
constructor or not and if the function happens to be a constructor, it
makes no difference to the properties of the function whether it has
actually been used to instantiated any Objects or not.

I think that properties of the constructor function most closely match
the way that static class members work in Java. Using similar syntax
(except that you don't have the option of omitting the class name from
within the class, but good Java style recommends the use of the class
name anyway) and with static methods having the same (zero) knowledge of
class instances.

However, you seem to have a very specific requirement in mind that may
be better served with an approach that exploits the object's prototype.
It might be an idea to provide some more information on how you intend
to employ your static variables.

Richard.


Richard Cornford

unread,
Apr 22, 2003, 12:15:35 PM4/22/03
to
Laurent Bugnion, GalaSoft wrote in message
<3EA5650F.5040209@bluewin_NO_SPAM.ch>...

>Note however that "static" members created this way are always
>public. There is, AFAIK, no way to create private static members
>in JavaScript.


And no - static final - either.

Richard.


Richard Cornford

unread,
Apr 22, 2003, 2:18:55 PM4/22/03
to
Richard Cornford wrote in message ...
<snip>
>myObject.aFunction = function(){
> //static function body!
> //Note: this function has no knowledge of any
> //instances of myObject and 'this' within this function
> //is a reference to the global object.
>};


I should not have bothered correcting myself as the above statement is
wrong. Looking at the code I realised that in a function assigned as a
method of a function object the 'this' keyword should be a reference to
the function object (the constructor). I tested that and it is.

At least it means that static class functions have the option of
referring to other properties of the constructor function as -
this.propName -.

Richard.

Richard Cornford

unread,
Apr 23, 2003, 3:47:56 PM4/23/03
to
Laurent Bugnion, GalaSoft wrote in message
<3EA5650F.5040209@bluewin_NO_SPAM.ch>...
<snip>

>There is, AFAIK, no way to create
>private static members in JavaScript.


Thinking to myself that the private members of an instance are possible
because the constructor can hold them in a closure, I started wondering
whether you could have private class members by having the class
constructor returned from a one-off function call that could form a
closure that gave the constructor function (and privileged class
methods) access to variables defined in the function that returned the
constructor.

I did a bit of testing and it seems that this idea will work.

The following HTML shows the basic principal, the object is completely
useless but it shows that this can be done. I will have to think up a
real application of JavaScript private static members to see if the idea
has any real worth and iron out any wrinkles that may show up.

<html>
<head>
<title></title>
<script type="text/javascript">

var myObject = function(){
var instArr = []; //private class array (of object instances).
function getNoOfInstances(){ //private class method.
return instArr.length;
};
function addInstance(inst){ //private class method.
var index = instArr.length;
instArr[index] = inst;
return index;
};
function cnst(id){ //class constructor.
this.id = id;
var self = this;
//call private class method and assign the returned
//index to a private instance member.
var index = addInstance(this);
this.getIndex = function(){ //privileged instance method.
return index;
};
};
cnst.getInstArr = function(){ //privileged class method
return instArr;
};
return cnst; //return the constructor.
}(); //simultaneously define and call!

myObject.prototype.getInstanceIndex = function(){
return this.getIndex();
}

var inst1 = new myObject('instance1');
var inst2 = new myObject('instance2');

</script>
</head>
<body>
<p>
<script type="text/javascript">
document.write(inst1.id+'<br>');
document.write(myObject.getInstArr().length+'<br>');
document.write(inst1.getInstanceIndex()+'<br>');
document.write(inst2.getInstanceIndex()+'<br>');
</script>
</p>
</body>
</html>

Richard.


John G Harris

unread,
Apr 23, 2003, 4:15:58 PM4/23/03
to
In article <pSapa.175056$UR.16...@news.chello.at>, Bela
<ro...@interware.hu> writes

>Hi All,
>
> Please help me, How can I create a static class variable in the
>JavaScript, maybe with prototype?

A third way to do it is

var MyClassStatics = new Object();
MyClassStatics.x = 42;

Now, as usual in javascript, you have a choice of three ways to get at
the "class static" value :

MyClass.x // using the constructor function object
a.x // using the prototype
MyClassStatics.x // using a separate object

Which is best for your program ? Only you can decide.

John
--
John Harris
mailto:jo...@jgharris.demon.co.uk

0 new messages