Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Access Static Variables Using Instances

Tags:

oop

typescript

So in most OOP languages static variables can also be called class variables, ie their value is shared among all instances of this class. For example, in my game I have a class Bullet which is extended by GreenBullet and PinkBullet. I want these subclasses to have a "class" or "static" variable called ammo so that I can keep track of the ammo count for that specific ammo type. But here is the catch: I want to be able to access this property through an instance of the subclass.

Example:

var bullet: GreenBullet = new GreenBullet()
if (bullet.ammo <= 0)
    return;
bullet.shoot();
bullet.ammo --;

I want ALL instances of GreenBullet to be aware of this change to their ammo count.

like image 691
Nigh7Sh4de Avatar asked May 23 '15 00:05

Nigh7Sh4de


People also ask

Can you access a static variable from an instance?

Can we access static variables from instance and static methods? Yes, static members (static variables) can be accessed from both instance and static area (i.e. instance and static methods) directly using the class name or without the class name. But outside the class, we can call only using class name.

How do you access static members of a class TypeScript?

ES6 includes static members and so does TypeScript. The static members of a class are accessed using the class name and dot notation, without creating an object e.g. <ClassName>. <StaticMember>. The static members can be defined by using the keyword static.

How do you call a static method in TypeScript?

A static method uses the static keyword instead of the function keyword when we define it. Static members can be encapsulated with the public, private and protected modifiers. We call a static method directly on the class, using the class name and dot notation. We don't need to create an object instance.

Can static variables be accessed anywhere?

Any object can access the static variables. They can also be accessed through static methods. If the variables are public, they can be accessed anywhere. Static variables are useful to keep information that is common to every object.


1 Answers

First option is to create instance accessors to static variable:

class GreenBullet
{
   static ammo: number = 0;
   get ammo(): number { return GreenBullet.ammo; }
   set ammo(val: number) { GreenBullet.ammo = val; }
}
var b1 = new GreenBullet();
b1.ammo = 50;
var b2 = new GreenBullet();
console.log(b2.ammo); // 50

If you want all subclasses of Bullet (including itself) to have separate ammo count, you can make it that way:

class Bullet
{
   static ammo: number = 0;
   get ammo(): number { return this.constructor["ammo"]; }
   set ammo(val: number) { this.constructor["ammo"] = val; }
}
class GreenBullet extends Bullet { }
class PinkBullet extends Bullet { }

var b1 = new GreenBullet();
b1.ammo = 50;
var b2 = new GreenBullet();
console.log(b2.ammo); // 50
var b3 = new PinkBullet();
console.log(b3.ammo); // 0

On a side note, I'm fairly sure you should not store bullet count in a static variable.

like image 182
zlumer Avatar answered Oct 13 '22 01:10

zlumer