javascript - 在递归循环中调用 setter
问题描述
我有一个 Javascript 类,它是:
class mockUser {
constructor (
location,
) {
this.location = location;
}
set location (data) {
this.location = data; // here's the problem
}
}
但是当我尝试使用set
关键字时,我会收到一个警告,说Function location recurses indefinitely and can only end by throwing an exception
. 这是为什么?
我的意思是我可以
setLocation (data) {
this.location = data; // here's the problem
}
但那有什么意义set
呢?
我想我误解了什么。
解决方案
“设置”的意思是“当某些东西试图设置这个属性时,调用这个函数”。
因此,通过在 setter 中设置位置,您正在调用 setter - 因此是递归。
推荐阅读
- c++ - 转换构造函数和运算符都存在并且涉及显式性时的行为
- flutter - 如何在与firebase一起使用电话号码进行身份验证?
- ios - 当我调用 URLSessionTask 的 task.cancel() 时会发生什么?
- android - 创建 Firebase 列表适配器时获取“类 FirebaseListAdapter 中的构造函数 FirebaseListAdapter 不能应用于给定类型”
- python - 如何使用 Foursquare API 收集有关场地评分的数据
- c# - Xamarin.Forms:如何在 C# 代码中使用自定义字体
- ggplot2 - x 轴组之间的间距大于组间距 geom_col
- python - ImportError:没有命名的模块
- azure - 在 azure-storage-blob 中 uploadFileToBlockBlob 时由对等方重置连接
- android - 键盘弹出时可滚动布局,但没有焦点更改