Why do class variables in Javascript disappear when trying to call them multiple times or assigning them to local variables?

家住魔仙堡 提交于 2019-12-08 03:56:44

问题


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);

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.


回答1:


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

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

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



来源:https://stackoverflow.com/questions/11791421/why-do-class-variables-in-javascript-disappear-when-trying-to-call-them-multiple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!