Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
除非使用速记,否则If语句无法定义变量(javascript)_Javascript_Reactjs_If Statement - Fatal编程技术网

除非使用速记,否则If语句无法定义变量(javascript)

除非使用速记,否则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

我试图定义一个变量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 * 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