使用 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP