英文:
Getter not invoked when chained in multiple assign operator
问题
当我使用多个赋值运算符,比如a = b = c
,而b
是一个getter/setter时,setter会被完美调用,但getter不会被调用;为什么?
这里有一个简单的示例:
// 定义一个简单的类来描述这种情况
export class GetterSetter {
private m_sValue: string = '';
constructor() {
}
public get value(): string {
return this.m_sValue;
}
private set value(val: string) {
this.m_sValue = (val ?? '').trim();
}
public getValue1(): string {
let ret: string = this.value;
this.value = null!;
ret = this.value;
return ret;
}
public getValue2(): string {
let ret: string = this.value;
ret = this.value = null!;
return ret;
}
}
// 调用
const obj: GetterSetter = new GetterSetter();
const s1: string = obj.getValue1();
const s2: string = obj.getValue2();
getValue1()
按预期工作。getValue2()
会产生null。在getValue2()
中,setter被调用了两次,但getter从未被调用。
对于getValue2()
,我期望在这一行中调用getter:
ret = this.value = null!;
但实际上并没有调用。这在调试模式和生产环境中都会发生。
英文:
When I use multiple assignment operator such as a = b = c
and b
is a getter/setter, the setter is invoked perfectly but getter not; why?
Here's a simple example:
//define simple example class to describe the situation
export class GetterSetter
{
private m_sValue:string = '';
constructor ()
{
}
public get value (): string
{
return this.m_sValue;
}
private set value (val:string)
{
this.m_sValue = (val ?? '').trim();
}
public getValue1 (): string
{
let ret:string = this.value;
this.value = null!;
ret = this.value;
return ret;
}
public getValue2 (): string
{
let ret:string = this.value;
ret = this.value = null!;
return ret;
}
}
//invocation
const obj:GetterSetter = new GetterSetter();
const s1:string = obj.getValue1();
const s2:string = obj.getValue2();
getValue1() works as expected. getValue2() produces null. In getValue2() the setter is invoked twice as expected but the getter never invoked.
For getValue2() I expected the getter to be invoked for this line:
ret = this.value = null!;
but it was not. It happens both in debugger and in production.
答案1
得分: 1
以下是您要的翻译:
"The JavaScript assignment expression y = x
evaluates to its right-hand side, so y = x
can be thought of as just x
. But it also has the effect of assigning the right-hand side to the variable/property on the left-hand side. So after y = x
is done, x
has been assigned to y
. Note that nowhere in that description is y
ever evaluated or read from; it is only written to. Code that only triggers when y
is read from will not trigger."
"在复合语句中,比如 z = y = x
,或者等效的 z = (y = x)
,首先发生内部赋值 y = x
,它将 x
赋给 y
并计算为 x
。然后外部赋值发生,将 x
赋给 z
(也计算为 x
但会被丢弃)。再次强调,y
(或者 z
)在任何时候都没有被读取,所以任何通过读取 y
触发的代码都不会执行。"
"因此,z = y = x
不像“x
被赋给 y
,然后 y
被赋给 z
”那样工作。相反,它的行为类似于“x
被赋给 y
和 z
”。"
英文:
The JavaScript assignment expression y = x
evaluates to its right-hand side, so y = x
can be thought of as just x
. But it also has the effect of assigning the right-hand side to the variable/property on the left-hand side. So after y = x
is done, x
has been assigned to y
. Note that nowhere in that description is y
ever evaluated or read from; it is only written to. Code that only triggers when y
is read from will not trigger.
In a compound statement like z = y = x
, or the equivalent z = (y = x)
, first the inner assignment y = x
happens, which assigns x
to y
and evaluates to x
. Then the outer assignment happens, which assigns x
to z
(and also evaluates to x
but that is simply discarded). Again, at no point is y
(or z
for that matter) ever read, so any code triggered by reading y
will stay untriggered.
So there you go. z = y = x
does not behave like "x
is assigned to y
and then y
is assigned to z
". Instead, it behaves like "x
is assigned to both y
and z
".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论