Class keyword in Javascript

前端 未结 7 1563
时光取名叫无心
时光取名叫无心 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:14

    Just to add the ECMA5 way of class making.

    Note that it does not have a constructor function this way (but you can trigger an init function if you like)

    var Class = {
          el: null,
          socket: null,
    
          init: function (params) {
    
            if (!(this.el instanceof HTMLElement)) {
              throw new Error('Chat room has no DOM element to attach on.');
            }
    
            return this.doStuff();
    
          },
          doStuff: function (params) {
            return this;
          }
    };
    
    var instanceofClass = Object.create(Class, {
      el: {
        value: document.body.querySelector('.what ever')
      },
      someMoreData: {
        value: [0,5,7,3]
      }
    }).init();
    
    0 讨论(0)
提交回复
热议问题