javascript - Javascript Setter 不是函数错误
问题描述
当我尝试在控制台上记录 isCheckedOut 设置器时,出现错误 testLib.isCheckedOut is not a function
我很难弄清楚为什么。任何帮助都会非常好
/* Parent Class */
class Library {
constructor(title) {
this._title = title;
this._isCheckedOut = false;
this._ratings = [];
}
get title() {
return this._title;
}
get isCheckedOut() {
return this._isCheckedOut;
}
set isCheckedOut(value) {
this._isCheckedOut = value;
}
get ratings() {
return this._ratings;
}
getAverageRating() {
}
toggleCheckOutStatus() {
}
addRating() {
}
}
const testLib = new Library;
console.log(testLib.isCheckedOut(true));
解决方案
Setters obfuscate the fact that they're functions to callers. When you have an object with a setter, to invoke the setter, assign to the property:
someObj.theSetterPropertyName = theArgumentToPassToSetter;
Similarly, to invoke a getter, reference the property as an expression:
someObj.theGetterPropertyName
So, you want:
class Library {
constructor(title) {
this._title = title;
this._isCheckedOut = false;
this._ratings = [];
}
get title() {
return this._title;
}
get isCheckedOut() {
return this._isCheckedOut;
}
set isCheckedOut(value) {
this._isCheckedOut = value;
}
get ratings() {
return this._ratings;
}
}
const testLib = new Library;
testLib.isCheckedOut = true; // invoke setter
console.log(testLib.isCheckedOut); // invoke getter
推荐阅读
- c# - VS 2019 中的大规模 NuGet 包管理器崩溃
- c# - 在剃刀页面中找不到 Web.Pages.Shared.Components 剃刀组件
- wordpress - shell_exec / require_once / 插件 add_action 没有工作
- c - C:指向函数中结构数组的指针
- java - java.io.IOException: Can't process class [class path] (Unsupported version number [55.0] (maximum 54.0, Java 10))
- node.js - nginx web 应用程序和 api 与 aws 负载均衡器和 aws ssl 证书一起使用
- python-3.x - 无法在 Windows 10 和 Python 3.7 上安装 dlib
- css - 在悬停时将超链接设置为下划线(无阴影效果)
- javascript - 在全屏模式下禁用 IOS Safari 中的捏合/缩放
- ruby - ruby 中 S3 Presigned url 中的文件名如何编码