使用 JavaScript 中的 get 來建立 getter 函式


使用 get 關鍵字建立 getter 函式。

示例

以下為程式碼 −

const studentDetails =
{
   studentName: "David Miller",
   get studentName() {
      console.log('I am calling the getter method...')
   }
}
console.log(studentDetails.studentName);

要執行上述程式,需要使用以下命令 −

輸出如下 −

node fileName.js.

此處,我的檔名是 demo221.js。

輸出

輸出如下 −

PS C:\Users\Amit\JavaScript-code> node demo221.js
I am calling the getter method...
Undefined

更新於: 2020-10-03

153 次瀏覽

開始你的職業

完成課程獲取認證

立即開始
廣告
© . All rights reserved.