1// the static method is a method which cannot be called through a class instance.
2class Point {
3 constructor(x, y) {
4 this.x = x;
5 this.y = y;
6 }
7
8 static distance(a, b) {
9 const dx = a.x - b.x;
10 const dy = a.y - b.y;
11
12 return Math.hypot(dx, dy);
13 }
14}
15
16const p1 = new Point(7, 2);
17const p2 = new Point(3, 8);
18
19console.log(Point.distance(p1, p2));
1class myClass{
2
3constructor(){
4this.myLocaleVariable=1;//this.varname in the constructer function makes a locale var
5}
6localfunction(){
7return "im local unique to this variable";
8}
9static publicfunction(){
10return "i can be called without an obj"
11}
12
13}
14myClass.myPublicVariable = 0;
15
16
17
18myClass.localfunction();//error
19myClass.publicfunction();//"i can be called without an obj"
20myClass.myLocaleVariable;//error
21myClass.myPublicVariable;//0
22var obj = new myClass;
23obj.localfunction();//"im local unique to this variable"
24obj.publicfunction();//error
25obj.myLocaleVariable;//1
26obj.myPublicVariable;//error
27