I’m really confused. If TypeScript transpiles to JavaScript and JavaScript is a classless language, then how can TypeScript have static methods?
As far as I understand, which could be wrong, in OOP languages supporting classes, a static method is part of the class which does not depend in any way on a particular instance of the class. A class is a piece of code, a blueprint, that contains enough information for cloning itself in the memory during runtime and a static method is part of that blueprint from the very beginning.
But JS does not support classes. Everything in JS is an object. So, there’s no blueprint. There is something in memory that is replicated times and times again whenever we use the new keyword in JS/TS. So, the concept of the keyword static is very confusing to me in this case.
I would appreciate if someone clears up these concepts and how they are related to each other and why I’m wrong.
3
Answers
Let’s see how would
static
look in ES5, before ‘class’ and other cool features were introduced.So you have some class (in ES5 that would be a function with accepting arguments like a constructor) e.g. named Person.
To add static method you would just assign some property to this class, so it will look like:
And this is it.
getSpecies
is a static method of a classPerson
. It’s a standalone property of an object.Using ESNext syntax it will look smth like:
JavaScript is not a classless language. Here’s the documentation on classes on JavaScript
You may be confused because JavaScript did not always have classes. But it turns out, you can emulate a class like system with the prototype based system pretty easily. So this difference doesn’t really matter most of the time. Even before official support for classes was added, you could still easily have what most would consider a "static method".
But all modern JavaScript engines support the
class
keyword for traditionalOOP
styles.Correct.
It actually not part of that blueprint at all. It’s part of the factory that produces that blueprint. So it belongs to the constructor, not the instances. An instance does not have direct access to static method.
If you had a class named
Person
and a static method to find all person records from a database, then a singlePerson
instance would never need to find all the people from the database. That belongs to the concept that is the collection allPerson
s and not any instance.So static methods aren’t part of the blue print because they are not part of what the instance has to work with.
It’s simpler than you think. A
class
is a constructor of object that all have similar interface and behave a certain way. Astatic
member of that class is simply a value that belongs to that constructor and never belongs to instances.This is a static method named
findAll
:And so is this: (although this is much hard for poor TypeScript)
In both cases here, there is a constructor that can produce instances which have an instance method. And that constructor also has a static method.
All that changes when you add the
static
keyword is that the method becomes available on the constructor, and is no longer available on instances.Skipping the things like
JS has classes since 2015
and others,Everything in JS in an object.
A Function is anso an object
So the function can have properties
And a "static" function is a property of the function itself rather then it’s
prototype