static Array in typescript
11,540
You should use class name instead of "this":
class A {
private static arrayname: string[] = ["a", "b", "c"];
someFunc(): number {
return A.arrayname.length; // Here I'm calling private statis property
}
}
var instanceA = new A();
alert(instanceA.someFunc());
This code works for me in the Typescript playground.
Note
You can use private member inside class functions only. It isn't accessible and visible outside the class declared in.
Comments
-
wazza almost 2 years
Hi I am new to Typescript and I need to use static array in my code. But when I declared array as follows
private static arrayname:String[];
When I typed
this.
it didn't show me the array name so I can't able to push any values into this array. Can any one help me in this? -
wazza almost 8 yearsI tried that too my class name is Sample but when I tried Sample. it doesn't shows the array name
-
wazza almost 8 yearsnow I can push data but I want it to use it outside the class hence I have changed to public but the array is not visible outside
-
TSV almost 8 yearsPublic members defenitely are visible from outside the class. The problem can be if classes are declared in different namespaces.