Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript static variable in class

class Thing {
  static type = 'thing';
  static myType() {
    return `This class has a type of ${this.type}`;
  }
}
console.log(Thing.type);
//=> 'thing'
console.log(Thing.myType());
//=> 'This class has a type of thing'

// Instances do not inherit static fields and methods
const t = new Thing();
console.log(t.type);
//=> undefined
console.log(t.myType())
//=> Uncaught TypeError: t.myType is not a function 
Comment

javascript static class variable

class ClassWithStaticMethod {
  static staticProperty = 'someValue';
  static staticMethod() {
    return 'static method has been called.';
  }
  static {
    console.log('Class static initialization block called');
  }
}

console.log(ClassWithStaticMethod.staticProperty);
// output: "someValue"
console.log(ClassWithStaticMethod.staticMethod());
// output: "static method has been called."

//------------------syntex-------------------------

static methodName() { /* ... */ }
static propertyName [= value];

// Class static initialization block
static {

}
Comment

static in class javascript

// static in class javascript
// Static class methods are defined on the class itself.
// You cannot call a static method on an object, only on an object class.
class Car {
  constructor(name) {
    this.name = name;
  }
  static hello() {
    return "Hello!!";
  }
}

let myCar = new Car("Ford");

// You can call 'hello()' on the Car Class:
document.getElementById("demo").innerHTML = Car.hello();

// But NOT on a Car Object:
// document.getElementById("demo").innerHTML = myCar.hello();
// this will raise an error.
Comment

PREVIOUS NEXT
Code Example
Javascript :: NaN 
Javascript :: divide symbol javascript 
Javascript :: array.slice 
Javascript :: how to set default value in input field in angularjs 
Javascript :: validate email or phone js 
Javascript :: data table is not a function in vue 
Javascript :: use inline and other styles react native 
Javascript :: expres body parser 
Javascript :: vue 3 create app 
Javascript :: reverse a string while keeping spaces in javascript 
Javascript :: array flat 
Javascript :: reactjs cdn file 
Javascript :: js fetch json 
Javascript :: ${} js 
Javascript :: react js form radio input using hooks 
Javascript :: javascript read consol input 
Javascript :: js check if all array values are the same 
Javascript :: object copy in javascript 
Javascript :: clear input field data in jquery 
Javascript :: array index javascript show only first 2 elements 
Javascript :: wait for 1 second in loop in javascript 
Javascript :: appendchild javascript 
Javascript :: svg to png base64 javascript 
Javascript :: vim go back word 
Javascript :: Remove duplicate items in an array 
Javascript :: javascript array flatten 
Javascript :: vue js hooks 
Javascript :: react-floating-whatsapp 
Javascript :: create a simple website using javascript 
Javascript :: node package.json type module 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =