使用 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