1

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

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? Commented Mar 9, 2011 at 10:55
  • No, Javascript does not have classes. There are widely-used hacky ways to fake them, though. 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? 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? 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? Commented Mar 9, 2011 at 20:08

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Just create a global variable.

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

3 Comments

I think he needs something to be the same for all instance of users using a website.
+1. An unhealthy obsession with "making everything an object" (even in non-OO languages! wtf?!) is the problem here. Just make it a global variable as Jakub says, and move on.
@Ali: I see no evidence of that.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.