zoukankan      html  css  js  c++  java
  • [ES7] Private, Static class Members

    Morden Javascript allows us to write private and static class memebers.

    To do this, we need to install babel plugins:

     

    First, let see "static member":

    class Cart {
      static name = "My Cart";
      
    }

    console.log(Cart.name)

    This is enabled by @babel/plugin-proposal-class-properties.

    Then let's see private method:

    class Cart {
      static name = "My Cart";
      #items;
      constructor(items = []) {
        this.#items = Object.freeze(items);
      }
      add(item) {
        const state = [...this.#items, item];
        this.#items = Object.freeze(state);
      }
      remove(id) {
        const state = this.#items.filter(item => item.id !== id);
        this.#items = Object.freeze(state);
      }
    }

    By adding "#" sign to the variable, we can convert this variable to a private prop.

    If you console log "new Cart()" you won't see "#items".

  • 相关阅读:
    HDU
    C# Stopwatch
    RMQ(Range Minimum Query)问题(转)
    HDU
    POJ
    HDU
    POJ
    POJ
    docker安装testlink
    廖雪峰Java2面向对象编程-3继承和多态-2多态
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13933619.html
Copyright © 2011-2022 走看看