0

I'm familiar with static and instance variables in java.

I can see javascript doesn't have the keyword 'static'

I can see that a function and the variables it can see outside of itself are rather like an object.

Experimenting at the javascript console, I see that

this.rrr= will create a variable outside the function, and if the function was called with new, then it creates an instance variable - a variable within the context of the object. Whereas if the function was called without new, then it just sets the variable outside of itself, there is no object, so it's essentially a static variable that it is setting.

Of course, no doubt as with java, (if I recall), an object can refer to a static variable, but a static method cannot refer to an instance variable. So if we call the function without 'new' then there's no instance variable for it to refer to. But if we call the function with new, then it should be able to refer to instance or static variables. And since in javascript it is possible to create variables outside of the function, it should perhaps be possible to create either instance variables or static variables.. I know that when the function is called without new, you can create a static variable. And when it's created with new, you can create an instance variable.. But can you also create a static variable when the function is created with new?

I set a variable rrr=1 it's essentially a static variable as it's not part of any object.

>rrr=1
1

I know that this.rrr could set an rrr variable within a p object, or could set a static rrr variable. And I know that the name asd() is superfluous and the function could be and may as well be anonymous.

>var p=function asd() {this.rrr=55;}


>p();

>rrr
55

>rrr=1
1

I can see that in the above, p() set the static variable rrr to 55 I then set rrr to 1 above

>rrr
1


>var q=new p(); 

>rrr
1

I know that q.rrr would have been set by the above.

>q=p();

>rrr
55

None of the above results surprise me. It's all as expected. But the examples clarify my question.

Is it possible, to call q=new p(); And have anything written in the p function, that sets the static rrr?

Like how an instance method in java can set a static variable, in java it does it by specifying the class name dot variable name, vs using 'this'. I wonder if javascript has a way?

2
  • 1
    this.rrr=55; inside your function does not really create a “static variable”, at least not if you mean that analog to what a static class property in Java would be. this simply refers to the global window object, and thus you just created window.rrr, so it it just a run-of-the-mill global variable. Had you just assigned a value to rrr directly inside your function, the effect would have been the same … Commented Jul 25, 2015 at 0:40
  • @CBroe ah, so rrr=5 in the function doesn't create a variable scoped to the function.. interesting.. and is same as window.rrr You can post that as an answer. What's the term then for an instance variable in javascript? like what this.rrr= creates when the function is called with 'new'? Commented Jul 25, 2015 at 0:47

1 Answer 1

2

this.rrr=55; inside your function does not really create a “static variable”, at least not if you mean that analog to what a static class property in Java would be. this simply refers to the global window object (because you are not in an “object context” here, as you would be with new p(), and thus you just created window.rrr, so it it just a run-of-the-mill global variable. Had you just assigned a value to rrr directly inside your function, the effect would have been the same …

What's the term then for an instance variable in javascript? like what this.rrr= creates when the function is called with 'new'?

Consider this example:

function Foo() {
    this.bar = 1;
    var baz = 2;
}

var someObj = new Foo();

console.log(someObj.bar); // this will get you 1
console.log(someObj.baz); // this will only get you undefined

So this.bar in object context is comparable to a public property, accessible from the outside, whereas var baz is local to the function scope (because of the var keyword), and therefor that is comparable to a private property.

I’d rather say “comparable”, not exactly the same … since JavaScript does not really now the concept of a “class”, but (function) objects only, applying the same terminology as in “classical” OOP (ha, pun intended) might not make that much sense.

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

3 Comments

ok so the equivalent of instance variables are variables in the object context, and the equivalent of static variables, are variables in the window context. You could add into the function a baf=3; with no var, or window.baf to show examples of the variable in the window context. So that your function shows all 3. The function scope/context, the global scope/context, the object scope/context
I see window.variablename= vs variablename= distinguishes between the parameter and the creation of one outside
“and the equivalent of static variables, are variables in the window context” – no, not really; they are not tied to your “class” (which would be our function here) in any way. Any part of the script can access them (so you not create a private static variable that way), and if two different “classes” both assign a value to foo, then it would be overwritten.

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.