有沒有一種簡單的方法可以在ES6及以上的類中生成只讀值?

Question

目前,我正在嘗試為我的類創建一個只讀值,這樣就沒有人可以編輯這些變量并弄亂類。

在我的類中,唯一的值可以是1、0、true或false,如果它們可以在任何時候編輯任何值,它們就可以將var設置為out-of-scope值,這可能會導致代碼錯誤!

    let Bit1 = new Bit(1);

    Bit1.Value = "fdgdf" // <-- You Should Not be able to do this!!

    ...

我想到了一種解決這個問題的方法,就是創建一個允許您更改值的函數,這樣它就不會是out-of-scope。

    Bit1.ChangeValue(true) // Accepted Value

    Bit1.ChangeValue("agjg") // Should throw Error: "Error: Value has to be ether a 1, 0 or an Boolean"

    Bit1.Value = "hbkij" // <-- You should not be able to do this!!

但正如您所見,您仍然可以編輯原始值而不會出錯!

很快,我注意到我需要一個read-only變量,我知道javascript因為MDN而有readOnly變量,但我不知道如何生成只讀變量

Please Help!!

Extra Info

IDE: Codesandbox

Browser: Chrome

Full Code:

    class Bit {
      /**
       *
       * @param {(Boolean|Number)} Value A Byte Value (1, 0, true, false)
       */
      constructor(Value) {
        if (Value !== null) {
          if (typeof Value === Boolean) {
            this.Value = +Value;
          } else if (typeof Number) {
            if (Value === 1 || Value === 0) {
              this.Value = Value;
            } else {
              throw new Error("Error: Value has to be ether a 1, 0 or an Boolean");
            }
          } else {
            throw new Error("Error: Value has to be ether a 1, 0 or an Boolean");
          }
        } else {
          throw new Error("Error: Value has to be ether a 1, 0 or an Boolean");
        }
      }
    
      /**
       * @description Swiches the Bit (1 -> 0, 0 -> 1)
       */
      Switch() {
        this.Value = Boolean(this.Value) ? 0 : 1;
      }
    }
    
    //class Bytes {
    //  /**
    //   *
    //   * @param {Array} Bits Array Of up to 8 bits (1, 0, true, false, Bit)
    //   */
    //  constructor(Bits) {
    //
    //  }
    //}
    
? 最佳回答:

你可以使用Object.defineProperty(..)

下面是一個例子:

class Bit {
  constructor() {
    Object.defineProperty(this, 'value', {
      value: false,
      writable: false,
      configurable: true
    });
  }

  changeValue(newValue) {
    if (newValue === false || newValue === true || newValue === 1 || newValue === 0) {
      Object.defineProperty(this, 'value', {
        value: newValue,
        writable: false,
        configurable: true
      });
    } else {
      throw new Error('Incompatible value');
    }
  }
}

const bit = new Bit();

bit.value = 'something';
console.log(bit.value);

try {
  bit.changeValue('something');
} catch(e) {
  console.error(e.message);
} 
console.log(bit.value);

bit.changeValue(true);
console.log(bit.value);

即使bit.value = ..沒有任何效果,因為此屬性設置為configurable: true,它仍然可以使用Object.defineProperty刪除或編輯。

另一種方法是使用setter和getter,下面是一個示例:

class Bit {
  #value = false;

  set value(newValue) {
    if (newValue === false || newValue === true || newValue === 1 || newValue === 0) {
      this.#value = newValue;
    } else {
      throw new Error('Incompatible value');
    }
  }

  get value() {
    return this.#value;
  }
}

const bit = new Bit();

try {
  bit.value = 'something';
} catch(e) {
  console.error(e.message);
} 
console.log(bit.value);

bit.value = true;
console.log(bit.value);

主站蜘蛛池模板: 97精品一区二区视频在线观看| 福利电影一区二区| 无码人妻精一区二区三区| 精品一区精品二区| 中文字幕一区一区三区| 亚洲av片一区二区三区| 国产免费一区二区三区| 中文字幕色AV一区二区三区 | 国产丝袜无码一区二区视频| 国产乱码一区二区三区爽爽爽 | 亚洲AV无码一区二三区| 无码视频免费一区二三区| 亚洲爆乳无码一区二区三区| 国产99精品一区二区三区免费| 福利一区二区三区视频午夜观看| 美女视频一区三区网站在线观看| 蜜桃臀无码内射一区二区三区| 国产精品毛片VA一区二区三区| 久久久久国产一区二区三区| 亚洲日本一区二区一本一道| chinese国产一区二区| 国产aⅴ一区二区| 国产成人av一区二区三区在线观看 | 视频在线观看一区二区| 日韩视频免费一区二区三区| 国产欧美色一区二区三区| 精品少妇人妻AV一区二区| 亚洲一区电影在线观看| 中文字幕一区视频| 亚洲精品色播一区二区| 精产国品一区二区三产区| 亚洲一区二区三区成人网站| 无码中文字幕乱码一区| 韩国精品福利一区二区三区| 国产精品成人一区无码| 国产韩国精品一区二区三区| 亚洲日本中文字幕一区二区三区| 成人区人妻精品一区二区不卡网站| 国产品无码一区二区三区在线蜜桃 | 久久精品无码一区二区app| 国产午夜精品一区二区三区小说|