开发者

Javascript properties initialized incorrectly

开发者 https://www.devze.com 2023-03-11 21:17 出处:网络
This problem is driving me nuts because it just should not happen. I have gone over it two dozen 开发者_运维百科times and I can\'t find out why the properties are being automatically reset.

This problem is driving me nuts because it just should not happen. I have gone over it two dozen 开发者_运维百科times and I can't find out why the properties are being automatically reset.

Here is the code

  function BoundingBox(){
        this.intersectBounds = {sx : NaN , sy : NaN , sw : NaN , sh : NaN , dx : NaN , dy : NaN , dw : NaN , dh : NaN , intersects : false};
        console.log(this.intersectBounds);
  }

  for (var i = 0; i<100 ; i++){
    var box = new BoundingBox();

intersectBounds does NOT exist

it is then set to {sx : NaN , sy : NaN...} and then when I do

console.log(this.intersectBounds) the first two run throughs the results are as would be expected:

Object
dh: NaN
dw: NaN
dx: NaN
dy: NaN
intersects: false
sh: NaN
sw: NaN
sx: NaN
sy: NaN
__proto__: Object

afterwards, the values are integers, not NaN. If it is of any use, the values in one case are

Object
dh: 42
dw: 470
dx: 0
dy: 0
intersects: true
sh: 42
sw: 470
sx: 0
sy: 0
__proto__: Object

I don't understand how this could possibly be. this.intersectBounds is being overwritten every time. Is this a scope error? ie {} adds properties to this. Is this a NaN error? ie. does NaN behave differently than other primitives/objects?


I am not entirely sure what you're asking, but I will take a stab at it:

When you're using an if or any other conditional, make sure your expression is... an expression. You should be able to express your conditional in plan language. So, try saying this out loud:

if (this.intersectBounds) {
  // do something
}

"If the variable this.intersectBouds then do something."

If what? If it isn't set (probably what you mean)? If it isn't false (probably not what you mean)? If it isn't blue? You should always have some kind of comparison in your conditional expression so there is no doubt as to what condition you are testing for:

if (typeof this.intersectBounds != 'undefined') {
  // do something
}

"If the variable this.intersectBouds has a typeof of 'undefined', then do something."

Now we've got an expression that is able to be expressed in plain language. This may be a part of why you're hitting a bug, but even if it isn't, this is always good practice.

Now, as far as scope goes... the first time you run the code, this.intersectBounds would not be set if you hadn't declared it prior to this code's execution. The next time you ran the code, presuming this is the same instance of the object handling it as last time, the variable would still be populated with the value you set it last time (presuming no other code changed it).


afterwards, the values are integers, not NaN. If it is of any use, the values in one case are

After what? You don't show us the code it runs through so there is no way for us to figure out what it changing it.

And yes, NaN behaves very differently from other objects. NaN is not equal to anything, including NaN itself. NaN == NaN will always return false! If you are using this in comparisons that's probably what is causing your error.

In cases like this where you have a property that is defined but not set to a specific value you should use null instead of NaN.


Turns out the problem has to do with console.log(). Executions downstream were affecting the print statements. Here is roughly what was being executed:

var x = {one : NaN , two : NaN , ... , ehn : NaN};
console.log(x);
...
...
...
x[one]=1; 
x[two]=2;
...
x[ehn]=n;

and the console, at least in google chrome, would display the updated values 1 through n, instead of NaN.

I find this extremely annoying as I just spent two hours debugging absolutely nothing. One step fowards, two steps back...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号