admin管理员组

文章数量:1290939

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

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

Share Improve this question edited Aug 23, 2019 at 2:09 Tlaloc-ES 5,28210 gold badges54 silver badges104 bronze badges asked Mar 9, 2011 at 10:52 Rana IjazRana Ijaz 111 gold badge1 silver badge2 bronze badges 5
  • 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 Commented 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 Commented 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 Commented 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 Commented 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 Commented Mar 9, 2011 at 20:08
Add a ment  | 

3 Answers 3

Reset to default 3

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.

Just create a global variable.

<script>
    var myGlobalArray = [];
</script>

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

本文标签: How to make a static array in javascript classStack Overflow