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.

Share:
11,540
wazza
Author by

wazza

Hardcore Wayne Rooney and Manchester United Fan.

Updated on June 04, 2022

Comments

  • wazza
    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
    wazza almost 8 years
    I tried that too my class name is Sample but when I tried Sample. it doesn't shows the array name
  • wazza
    wazza almost 8 years
    now 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
    TSV almost 8 years
    Public members defenitely are visible from outside the class. The problem can be if classes are declared in different namespaces.