除非使用速记,否则If语句无法定义变量(javascript)
我试图定义一个变量x,用于检查圆形显示中移动点沿x轴的位置,如果超出允许半径,则重新打印。仅使用直线将其指定为一个方向时,它才起作用:除非使用速记,否则If语句无法定义变量(javascript),javascript,reactjs,if-statement,Javascript,Reactjs,If Statement,我试图定义一个变量x,用于检查圆形显示中移动点沿x轴的位置,如果超出允许半径,则重新打印。仅使用直线将其指定为一个方向时,它才起作用: let x = ((this.state.centerx[point] + frame * this.props.jump) < (0.5 * this.props.width - 0.15 * this.props.width)) ? (this.state.centerx[point] + frame * this.props.jump) + (0.3
let x = ((this.state.centerx[point] + frame * this.props.jump) < (0.5 * this.props.width - 0.15 * this.props.width)) ?
(this.state.centerx[point] + frame * this.props.jump) + (0.3 * this.props.width) : this.state.centerx[point] + frame * this.props.jump
以下是我尝试使用的全部代码:
for(var frame = 0; frame < this.props.numframes; frame++) {
var startpoint = (frame % 2 === 0) ? 0 : this.props.numpoints
for(var point = startpoint; point < startpoint + this.props.numpoints; point++) {
if(this.state.direction > 0) {
let x = ((this.state.centerx[point] + frame * this.props.jump) < (0.5 * this.props.width - 0.15 * this.props.width)) ? (this.state.centerx[point] + frame * this.props.jump) + (0.3 * this.props.width) : (this.state.centerx[point] + frame * this.props.jump)
} else {
let x = ((this.state.centerx[point] + frame * this.props.jump) > (0.5 * this.props.width + 0.15 * this.props.width)) ? (this.state.centerx[point] + frame * this.props.jump) - (0.3 * this.props.width) : (this.state.centerx[point] + frame * this.props.jump)
}
for(var frame=0;frame0){
设x=((this.state.centerx[point]+frame*this.props.jump)<(0.5*this.props.width-0.15*this.props.width))?(this.state.centerx[point]+frame*this.props.jump)+(0.3*this.props.width):(this.state.centerx[point]+frame*this.props.jump)
}否则{
设x=((this.state.centerx[point]+frame*this.props.jump)>(0.5*this.props.width+0.15*this.props.width))?(this.state.centerx[point]+frame*this.props.jump)-(0.3*this.props.width):(this.state.centerx[point]+frame*this.props.jump)
}
感谢您提供的任何见解。区别在于作用域。var的作用域是最近的函数块,let的作用域是最近的封闭块,可以小于函数块。如果在任何块之外,两者都是全局的 另外,在用let声明的变量在其封闭块中声明之前是不可访问的 尝试更改变量定义方法
let x
到
将每一条语句移动到它自己的变量中,你可能会有更轻松的时间,更好的可读性。你也因此而编写了多余的代码。
let x
const x