A class is made in javascript. I create its objects in differnt other scripts. But I need its one data member(array) to be static? every time I create a new object of that class then it should not lost that arrays data. I need old array data...
-
First of all: there are no classes in JavaScript. Do you mean that you want each object to start with a property set to an array of default values, but that later that array could be modified independently on each object?Jon– Jon2011-03-09 10:55:39 +00:00Commented Mar 9, 2011 at 10:55
-
No, Javascript does not have classes. There are widely-used hacky ways to fake them, though.Lightness Races in Orbit– Lightness Races in Orbit2011-03-09 10:57:19 +00:00Commented Mar 9, 2011 at 10:57
-
Is my understanding correct that by 'static' you mean varaibles that are the same no matter which user logs into your site or application?Ali– Ali2011-03-09 11:00:09 +00:00Commented Mar 9, 2011 at 11:00
-
@Ali - My understanding is that we are talking about static members, i.e. data which has persistence across all instances of a class. Not across all users or sessions within the whole system?johnhunter– johnhunter2011-03-09 11:20:53 +00:00Commented Mar 9, 2011 at 11:20
-
@johnnumber: In back-end code (lets say Java) isn't that how static members work? Aren't the the same across all sessions?Ali– Ali2011-03-09 20:08:15 +00:00Commented Mar 9, 2011 at 20:08
3 Answers
You can simulate static members with a property on the constructor. In the example below we have a psuedo-class with a constructor Foo. When we create an instance of Foo a reference to the instance is added to Foo's static instances property.
function Foo () {
this.description = ' this is the Foo class';
Foo.instances.push(this); // static member keeps ref to each instance
};
// this is an instance member
Foo.prototype.getDescription = function () {
return this.description;
}
// this is a static member
Foo.instances = [];
So what we're really talking about here is using the constructor as a 'namespace' to hold variables that are in some way related to the 'class'. Its better than using globals because its a little cleaner. As others have said, there are no classes as such but this pattern is conceptually close to static members if you like to think in a class-ical way.
Comments
Just create a global variable.
<script>
var myGlobalArray = [];
</script>
3 Comments
The trick is to create a private variable and define the prototype methods within the constructor, to be able to use the private variable in the closure.
So, this may approach what you want?
function Foo(){
var staticArr = [];
if (!('prototypemethodsset' in Foo)){
var proto = Foo.prototype;
proto.getArr = function(){
return staticArr;
};
proto.addArr = function(){
staticArr.push(Array.prototype.slice.call(arguments));
};
proto.prototypemethodsset = true;
}
}
var f = new Foo
, g = new Foo;
f.addArr(1,4,8,9,13,7);
g.addArr(5,7);
alert(g.getArr()); //=> 1,4,8,9,13,7,5,7
alert(f.getArr()); //=> 1,4,8,9,13,7,5,7