Static vs Instance in Javascript
source link: https://www.frontendroom.com/blog/static-vs-instance-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Static vs Instance in Javascript
Table of contents
Static properties and methods & instance properties and methods are related to the Class
in javascript.
In this article, we will learn the difference between these two (static and instance).
Static properties and methods:
Static properties and methods are those properties and methods which belongs to the class only.
It is directly available to the Class
.
We define them with static
word in class declaration.
For e.g.
class Car {
static noOfWheels = 4
static increase(speed) {
return ++speed
}
}
In this example, you can see that the property noOfWheels
is static property and the method increase
is a static method.
Another way to declare static properties and methods on the class
is by using the dot method.
For example:
class Car {}
Car.noOfWheels = 4
Car.increase = function (speed) {
return ++speed
}
Some Key Points:
- Static properties and methods belongs the
class
. - Static properties and methods of a class are available to instances of that class.
Instance properties and methods:
Those properties and methods which are attached to instances of the class i.e. objects, not to the class, are called instance properties and methods.
For example:
class Car {
speed = 0
increaseSpeed() {
return ++this.speed
}
}
const car1 = new Car()
console.log(car1.speed) // 0
console.log(car1.increaseSpeed()) // 1
In the above code, you can see that instance property speed
and instance method increaseSpeed
of object car1
Another way to declare this:
class Car {}
const car1 = new Car()
car1.speed = 0
car1.increaseSpeed = function () {
return ++this.speed
}
console.log(car1.speed) // 0
console.log(car1.increaseSpeed()) // 1;
The difference:
Static properties and methods belong to class
. Thus it will be available to all the instances of that class.
Instance properties and methods belong to the instance of the class
.
Thanks for reading the article. Keep coding and keep solving problems.
About Author:
Amrish Kushwaha
I am Amrish Kushwaha. Software Engineer, Maker, Writer. I am currently focused on frontend development with curiosity of building end to end software system. Currently I am working at Rafay Systems. I love to write as well as build side projects when I am free.
Recommend
-
73
Static site generators are like Netflix series. Just when you thought you'dseen them all, you find another one. And they all look great!
-
39
Image by
-
20
Static websites and so-called JAMstack have become pretty popular recently. And with 2020 on the horizon, this trend doesn't seem to be stopping. Why? Why is old-school HTML + CSS + JS trio in its rede...
-
6
This article was written in collaboration with Joe Previte, an Open Source Developer Advocate at Facebook. In this post, we explain
-
21
Python Instance, Class, and Static Methods ExplainedIt’s easier than you thought!
-
21
Why are the static variable data lost and are they not an instance when IIS / application pools are rebooted? advertisements Why is that if I...
-
3
Deep dive into public and private instance fields in JavaScript (ECMAScript 2022) Oct 21, 2021 , by Chetan Gawai 2 minute read ...
-
4
Javascript & ldquo; Private & rdquo; Vs properties of the instance advertisements I'm doing some Javascript R&D and,...
-
6
Do Java classes have a machine level instance (JVM) if they contain only static methods and fields? advertisements Do Java...
-
7
Why JavaScript class members are not automatically bound to the instance of the class · GitHub Instantly share code, notes...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK