英文:
I'm receiving maximum call stack size exceeded error on 2 identical defined classes, execept 1 is defining this.name and the other this._name
问题
在两个相同定义的类上,我收到了"Maximum call stack size exceeded"错误,唯一的区别是一个定义了 this.name 而另一个定义了 this._name。导致其中一个返回堆栈错误的是这两个类的区别是什么?
class User {
constructor(name) {
// 调用 setter
this.name = name;
}
get name() {
return this._name;
}
set name(value) {
if (value.length < 4) {
console.log("名字太短了。");
return;
}
this._name = value;
}
}
let user = new User("John");
console.log(user.name); // John
user = new User(""); // 名字太短了。
RangeError: Maximum call stack size exceeded
class User {
constructor(name) {
// 调用 setter
this.name = name;
}
get name() {
return this.name;
}
set name(value) {
if (value.length < 4) {
console.log("名字太短了。");
return;
}
this.name = value;
}
}
let user = new User("John");
console.log(user.name); // John
user = new User(""); // 名字太短了。
英文:
I'm receiving maximum call stack size exceeded error on 2 identical defined classes, execept 1 is defining this.name and the other this._name. What is the difference in the 2 classes that's causing one of them to return a class stack error?
class User {
constructor(name) {
// invokes the setter
this.name = name;
}
get name() {
return this._name;
}
set name(value) {
if (value.length < 4) {
console.log("Name is too short.");
return;
}
this._name = value;
}
}
let user = new User("John");
console.log(user.name); // John
user = new User(""); // Name is too short.
RangeError: Maximum call stack size exceeded
class User {
constructor(name) {
// invokes the setter
this.name = name;
}
get name() {
return this.name;
}
set name(value) {
if (value.length < 4) {
console.log("Name is too short.");
return;
}
this.name = value;
}
}
let user = new User("John");
console.log(user.name); // John
user = new User(""); // Name is too short.
答案1
得分: 1
get name() {
return this.name;
}
我认为这会导致无限递归函数..
当你调用
console.log(user.name);
它将无休止地调用getter。
设置器也发生了相同的情况..
英文:
get name() {
return this.name;
}
I believe this act as an infinite recursive function ..
When you call
console.log(user.name);
It just endlessly call the getter.
Same thing happening for the setter ..
答案2
得分: 0
第一个类将this._name
定义为数据属性。当构造函数设置this.name
时,它会传递给Setter,Setter会将this._name
的值设置为该值。现在当调用getter时,它会返回由setter先前设置的this._name
的值。
第二个类仅为this.name
定义了getter和setter。当构造函数尝试将this.name
设置为一个值时,setter也会尝试设置this.name
,这会导致setter被调用,然后setter的新实例尝试再次修改this.name
,然后递归限制达到最大值,就像这样。第二个类中的getter也是如此。它返回this.name
,从而调用自身,以这种方式填满堆栈,达到最大递归限制。
英文:
The first class defines this._name
as a data property.
When the constructor sets this.name
, it is passed to the Setter, which sets the value of this._name
to the value. When the getter is called now, it returns the value of this._name
, which was set by the setter earlier.
The second class only defines a getter and a setter for this.name
.
When the constructor attemts to set this.name
to a value, the setter will also try to set this.name
, which causes the setter to invoked, and the new instance of the setter is attempting to modify this.name
again, and then the maximum recursion limit is exceeded like this. Same goes for the getter in the second class. It returns this.name
thereby invoking itself, and this way, it fills up the stack to the maximum recursion limit.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论