For using a static variable in javascript functions I found out two ways, using . and : operator.
When using . operator we have to specify variable with “f.variable” and when using : we have to use “this.variable”. what is the difference between the usage of these two operators.
function f(){
f.a += 1;
this.b += 1;
console.log("f.a: ", f.a);
console.log("this.b: ", this.b);
}
f.a = 0;
f:b = 0;
also we cannot use : when using that variable outside its function like:
function g(){
f:b = 0; //this works fine.
var c = f:b; //raises error invalid label.
console.log(f:b);//but this raises an error missing ')'.
}
Same is the case when we use var to create objects.
var obj = {
a: 2,
b: 3
}
//accessing a and b is done using obj.a & obj.b
//but here
obj:a = 4;
console.log(f.a); // this gives 2
//and similarly using obj:a as rhs value gives error.
How are these two operators used actually.
EDIT:
What is the difference between these two types of variables created.
This:
is interpreted as a label, “f”, before an expression statement,
b = 0;. The “:” is used in object literal syntax to separate a property name expression from its value expression. Otherwise, it is not used for referring to properties of objects.