0

I want to create a static variable in my js. From what I know with my little js knowledge is, I can do it using this two ways.

function foo (){

    if (this.counter==undefined){this.counter=1}
    else {this.counter++ } ;

}

function foo (){

    if (foo.counter==undefined){foo.counter=1}
    else {foo.counter++ } ; 

}

Are these two things essentially the same or I need to to careful in selecting one versus other.

Another question I have is: why var is not needed when doing these?

7
  • you need to read about scope Commented Jul 19, 2013 at 13:56
  • I have no clue about the implementation in javascript, but usually a construction like foo.counter makes the variable counter available from any foo object. (e.g. var a = new Foo(); var b = new Foo(); a.counter === b.counter;. I am not sure if that is the case with javascript too. Commented Jul 19, 2013 at 13:56
  • Are you planning on using foo to create objects using new or just calling it as a function without the new? Commented Jul 19, 2013 at 14:04
  • My objective is to call it as a function. Commented Jul 19, 2013 at 14:05
  • 1
    @Jack_of_All_Trades: Then read MDN's introduction to the this keyword. You will soon realize that this is not what you want. Commented Jul 19, 2013 at 14:08

2 Answers 2

2

No, they are not equivalent. In your first example, you are using this. this can actually change depending on how the function is being called.

function showThis(){
    console.log(this);
}

var obj = { };
obj.showThis = showThis;

showThis(); // gives 'window', or the top-most scope of your page
obj.showThis(); // gives obj as 'this'

If you are always calling the function the same way, then this would just mean the value counter is tracked as a property on window.counter. This is bad, because you may accidentally have an actual variable named counter in that scope, that you are now using in a different way somewhere else. If you are not calling it the same way everytime, then this will be different and probably not giving you your desired behavior.

If you are trying to keep a count on the number of times foo is being called, independent of how/who is calling it, then your second approach is more appropriate. For the sake of code clarify/convention, I would write it this way:

function foo (){
    // If counter does not exist, create it and initialize it to 0.
    foo.counter = foo.counter || 0;
    foo.counter++;
}

foo.counter; // undefined, since foo has never been called yet
foo();
foo.counter; // is now 1
Sign up to request clarification or add additional context in comments.

2 Comments

This absolutely clarifies what I wanted to understand. Thanks Matt.
@Jack_of_All_Trades Glad to have helped!
1

var is used to create a variable in the current scope.

Both the approaches you are taking are setting a property on an object that exists in a wider scope.

2 Comments

Can you please clarify, what happens if I have var this.counter=1 rather than this.counter=1 in the first case?
You get an exception. var is used to declare variables, not properties on objects.

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.