tags:

views:

87

answers:

4
//my class
function myClass()
{
   this.pubVar = 'hello public';
   var priVar = 'hello private';


}

myClass.staticMethod = function()
{

   //how do i access thos variables here

}

I tried alert(this.pubVar) and alert(priVar) with no success. What am I doing wrong?

I'm going to change the question a bit.. I didn't explain it well the first time through.

How do I declare static variables in a class?

I know with java it's simple, you just put static infront of the variable type and presto you've got a static variable. I can't seem to find that option for javascript..

+2  A: 

Definition:

myClass.staticMethod = function(obj)
{
    alert(obj.pubVar);
}

Use:

var instance = new myClass();
myClass.staticMethod(instance);

You won't be able to use "priVar", since it isn't a variable on the class. It's a method-scoped variable.

Also, if you need to access properties of the class, why are you making it a static method?

John Fisher
I didn't really want to create a instance, I like calling a function by the classname.methodname()
payling
Then you definitely don't want to use a static method.
John Fisher
A: 

Hmm... If you are trying to access "this" then you don't really want a static method. A static method belongs to the class and doesn't have access to any given instance of that class, and therefore "this" does not exist in the context of a static method.

If you are looking for the code for an instance method it would be something along these lines:

function myClass()
{
    this.pubVar = 'hello public';
    var priVar = 'hello private';

    this.method = function(){
        alert(priVar);
    }
}

Alternatively, you could pass a reference to an object into your static method, which would look something like this:

function myClass()
{
    this.pubVar = 'hello public';
    var priVar = 'hello private';
}
myClass.staticMethod = function(obj)
{
    alert(obj.pubVar);
}

Hope that helps

LorenVS
so I guess the answer is no... I was trying to access it from the class so I didn't have to pass it through to every static method
payling
+1  A: 

Since you want to use the instance of the object inside of your method, I think that you are not really wanting to create a static method, what you want, is a method that is applicable and accessible by all the instances.

For doing that you can extend the constructor's prototype:

function MyClass() {
   this.pubVar = 'hello public';
   var priVar = 'hello private';
}

MyClass.prototype.myMethod = function() {
   alert(this.pubVar);
}


var instance = new MyClass();
instance.myMethod(); // alerts 'hello public'

When you extend the prototype of a constructor function, all the members that reside on it, will be inherited to the object instances created with the new operator.

So you define this method only once in your constructor function, and it will be accessible in the context of each instance (the this keyword inside the method refers to the object instance).

CMS
This is what I was gonna say. +1/
Triptych
A: 

It is really simple. Of cause you need an instance of class for this purpose:

//my class
function myClass()
{
   this.pubVar = 'hello public';
   var priVar = 'hello private';
}

myClass.staticMethod = function()
{
   //how do i access thos variables here
   alert(this.pubVar);
}

var instance=new myClass(); // this is instance of class
myClass.staticMethod.call(instance)

The only question do you really need to use static then? Also there are no means to access priVar. Because it uses closure.

Eldar Djafarov
I thought in java that you could declare static variables in a class and access them using static methods.
payling
I guess the question should have been how to declare static variables in javascript class..
payling