Answers for "what is the use of javascript static methods on classes"

0

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));
Posted by: Guest on August-08-2021
0

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' ]
Posted by: Guest on October-11-2020
0

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!
Posted by: Guest on October-21-2020
0

what is the use of javascript static methods on classes

// javascript static methods on Classes are called via the Class Name not  via the class Instances
class Square {
    constructor(width){
        this.width = width ;
        this.height = width ;
    }

    static equals(a , b){
        return a.width * a.height === b.width * b.height ;
    }
}


const newSquare1 = new Square(10);
const newSquare2 = new Square(9);
const newSquare3 = new Square(5);
const newSquare4 = new Square(5);
console.log(Square.equals(newSquare1 , newSquare2));
console.log(Square.equals(newSquare3 , newSquare4));
Posted by: Guest on August-08-2021

Code answers related to "what is the use of javascript static methods on classes"

Code answers related to "Javascript"

Browse Popular Code Answers by Language