javascript class static fields
class ClassWithInstanceField {
instanceField = 'instance field'
}
class ClassWithStaticField {
static staticField = 'static field'
}
javascript class static fields
class ClassWithInstanceField {
instanceField = 'instance field'
}
class ClassWithStaticField {
static staticField = 'static field'
}
what is the use of javascript static methods on classes
// Classes : static methods in javascript classes
class Square {
constructor(width){
this.width = width ;
this.height = width ;
}
static equals(a , b){
return a.width * a.height === b.width * b.height ;
}
static validDimensions(width , height ){
return width === height ;
}
}
console.log(Square.validDimensions(8 , 8));
console.log(Square.validDimensions(10 , 12));
console.log(Square.validDimensions(0 ,0 ));
console.log(Square.validDimensions(2 , 3));
console.log(Square.validDimensions(-3 , -3));
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
get static methods of class javascript
class Hi {
constructor() {
console.log("hi");
}
my_method(){}
static my_static_method() {}
}
function getStaticMethods(cl) {
return Object.getOwnPropertyNames(cl)
}
console.log(getStaticMethods(Hi))
// => [ 'length', 'prototype', 'my_static_method', 'name' ]
javascript class static method
// *** JS Class STATIC method ***
class Animal {
sayHello() {
return `${this.constructor.greeting}! I'm ${this.name}!`;
}
}
class Cat extends Animal {
constructor(name) {
super(); // connects parent to child/constructor
this.name = name; //=> need this line otherwise name undefined
}
static greeting = 'Feed me';
}
class Dog extends Animal {
constructor(name) {
super();
this.name = name; // same output type as above
}
static greeting = 'Sigh';
}
const run = document.getElementById("run");
run.addEventListener('click', () => {
let Garfield = new Cat('Garfield');
let Snoopy = new Dog('Snoopy');
console.log(Garfield.sayHello());
console.log(Snoopy.sayHello());
})
// output = Feed me! I'm Garfield! Sigh! I'm Snoopy!
js class static
class ClassWithStaticMethod {
static staticMethod() {
return 'static method has been called.';
}
}
console.log(ClassWithStaticMethod.staticMethod());
// expected output: "static method has been called."
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us