Answers for "static js"

2

static js

// the static method is a method which cannot be called through a class instance.
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  static distance(a, b) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;

    return Math.hypot(dx, dy);
  }
}

const p1 = new Point(7, 2);
const p2 = new Point(3, 8);

console.log(Point.distance(p1, p2));
Posted by: Guest on March-04-2021
2

js static methods

class Foo(){   static methodName(){      console.log("bar")   }}
Posted by: Guest on January-30-2021
2

js class static

class myClass{
 
constructor(){
this.myLocaleVariable=1;//this.varname in the constructer function makes a locale var
}
localfunction(){
return "im local unique to this variable";
}
static publicfunction(){
return "i can be called without an obj"
}
  
}
myClass.myPublicVariable = 0;



myClass.localfunction();//error
myClass.publicfunction();//"i can be called without an obj"
myClass.myLocaleVariable;//error
myClass.myPublicVariable;//0
var obj = new myClass;
obj.localfunction();//"im local unique to this variable"
obj.publicfunction();//error
obj.myLocaleVariable;//1
obj.myPublicVariable;//error
Posted by: Guest on April-11-2020
0

js class static

class ClassWithStaticMethod {
  static staticMethod() {
    return 'static method has been called.';
  }
}

console.log(ClassWithStaticMethod.staticMethod());
// expected output: "static method has been called."
Posted by: Guest on August-30-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language