为什么在JavaScript中的类变量在尝试多次调用它们或将它们分配给局部变量时会消失? [英] Why do class variables in Javascript disappear when trying to call them multiple times or assigning them to local variables?

查看:103
本文介绍了为什么在JavaScript中的类变量在尝试多次调用它们或将它们分配给局部变量时会消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var width = 10;
var data = image.data;
var height = 10;

for ( var x = 0; x < width; x++ ) {

  for ( var y = 0; y < height; y++ )

    var index = 4 * (y * height +  x); // 0

    var local_variable = ArbitraryClassInstance.getBrightness(x, y); // 0

    data[ index ] = ArbitraryClassInstance.getBrightness(x, y); // 0 for both index/call
    data[ index + 1 ] = ArbitraryClassInstance.getBrightness(x, y); // 0 for both index/call

  }

}

context.putImageData(image, 0, 0);

当我执行代码时,index和local_variable都等于零。图像是JavaScript图像对象,数据是图像数据,宽度和高度是图像的尺寸。该类返回亮度。

Both index and local_variable are equalled to zero when I execute the code. Image is a javascript image object, data is the image data, and width and height are the dimensions of the image. The class returns brightness.

推荐答案

想象出来。我没有关闭我的大括号。对所有人发出警告。

Figured it out. I didn't close my curly brackets. A warning to all.

Chrome / Firefox正常降级,不要引发错误!

Chrome/Firefox degrade gracefully and DON'T THROW AN ERROR! You will not believe how long I was chasing this bug down.

请检查您的语法 - javascript会在C中抛出错误(指针不会出现)。

Check your syntax - javascript throws errors like pointers in C (it doesn't).

这篇关于为什么在JavaScript中的类变量在尝试多次调用它们或将它们分配给局部变量时会消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆