I have the code that sets a property only if the argument is truthy:
export class Vector {
protected x: number = 0
protected y: number = 0
protected z: number = 0
set(x?: number, y?: number, z?: number) {
this.x = x ? x : this.x
this.y = y ? y : this.y
this.z = z ? z : this.z
}
}
However, I don't want to provide a fallback value of the current value. Ideally, I want to just do nothing if the value is falsy, like this:
if (x) {
this.x = x
}
if (x) {
this.y = y
}
if (z) {
this.z = z
}
… but I don't want to write it like this, it is not cool. I'm looking for something like this:
this.x = x ? x
this.y = y ? y
this.z = z ? z
Is there any similar syntax that does what I want?
a ? a : bis equivalent toa || b(unlessaexpression has side effects).this.x = x || this.xx && this.x = x; y && this.y = y;etc.