第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在類中實(shí)現(xiàn)獲取和設(shè)置

在類中實(shí)現(xiàn)獲取和設(shè)置

我通過w3s并發(fā)現(xiàn)了這一點(diǎn)。https://www.w3schools.com/Js/tryit.asp?filename=tryjs_classes_getters我理解所有值所采用的路徑,但是如何實(shí)現(xiàn)/為什么使用類中的get設(shè)置?我通過實(shí)施來學(xué)習(xí),我將不勝感激。
查看完整描述

1 回答

?
胡說叔叔

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊

一個(gè)可能的用途是驗(yàn)證。想象一下以這種方式實(shí)現(xiàn)的類:Account


class Account {

  #username;

  #password;

  constructor(name) {

    this.#username = name;

  }

  get username() {

    return this.#username;

  }

  set password(password) {

    if (password.length < 10) {

      throw "Password must be at least 10 characters long!";

    }

    this.#password = password;

  }

  get password() {

    throw "Forgotten passwords cannot be retrieved!  Please make a new one instead.";

  }

}

我可以創(chuàng)建一個(gè)新帳戶,如下所示:


const myAccount = new Account('John Doe');

如果我嘗試將密碼設(shè)置為不可接受的短長(zhǎng)度,我將收到一個(gè)錯(cuò)誤:


myAccount.password = 'hunter2'; // Password must be at least 10 characters long!

如果我在忘記密碼后嘗試找回密碼,我會(huì)再次收到錯(cuò)誤:


console.log(myAccount.password); // Forgotten passwords cannot be retrieved!  Please make a new one instead.


查看完整回答
反對(duì) 回復(fù) 2022-09-29
  • 1 回答
  • 0 關(guān)注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)