Class keyword in Javascript

前端 未结 7 1591
时光取名叫无心
时光取名叫无心 2020-12-03 04:17

According to this article it should be a Javascript 2.0 way to define class. However, I never saw that in practice. Thus the question. How to use class keyword and what is t

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 05:06

    I know this is a old post, but as of today i.e with the advent of ECMAScript 6 we can declare javascript classes.

    The syntax goes as follows :

    class Person{
      constructor(name){
        this.name = name;
      }
      printName(){
        console.log('Name is '+this.name);
      }
    }
    var john = new Person('John Doe');
    john.printName(); // This prints 'Name is John Doe'
    

    A complete guide to this can be found in this post

提交回复
热议问题