Please help me, How can I create a static class variable in the
JavaScript, maybe with prototype?
Thanks: R. Bela
function YourClass () {
//...
}
YourClass.prototype.classVariable = 'Kibology';
You can then access
this.classVariable
inside methods or
object.classVariable
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" <Ric...@litotes.demon.co.uk> az alábbiakat írta a
következő hírüzenetben: b83ih8$sq8$1$8302...@news.demon.co.uk...
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.
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';
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
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.
And no - static final - either.
Richard.
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.
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.
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