Why do class variables in Javascript disappear whe

2019-06-07 07:31发布

问题:

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