Complete Guide to Modern JavaScript by Alberto Montalesi

Complete Guide to Modern JavaScript by Alberto Montalesi

Author:Alberto Montalesi
Language: eng
Format: epub
Tags: Computers & Technology
Published: 2020-03-30T00:00:00+00:00


Static methods

Right now the two new methods that we created, greet() and farewell() can be accessed by every new instance of Person, but what if we want a method that can only be accessed by the class itself, similarly to Array.of() for arrays?

class Person {

constructor(name,age){

this.name = name;

this.age = age;

}

static info(){

console.log("I am a Person class, nice to meet you");

}

}

const alberto = new Person("Alberto",26);

alberto.info();

// TypeError: alberto.info is not a function



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.