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

5 Answers 5

4

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

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

1 Comment

This is what I was gonna say. +1/
3

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?

2 Comments

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

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

1 Comment

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
1

Sorry for the answer on a old post, may be this will help someone

If you want to access a variable from a static method, you can declare that variable as a static property.

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

myClass.staticProperty = 'someValue';

myClass.staticMethod = function()
{
    //This is how you access it
    alert(myClass.staticProperty);
}

Comments

0

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.

2 Comments

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

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.