javascript override undefined

拥有回忆 提交于 2019-12-11 19:24:07

问题


After searching many hours i dont find a solution for my problem:

function pseudoclass(Value){
    this.Value = Value;
}
aPlayGround = new Array();
aPlayGround[0] = new pseudoclass(0);
var Actual = 0;
var Width = 1;
var bBool = new Boolean(Actual%Width); //left becomes 0%1 ----> false
if (bBool && (aPlayGround[Actual-1].Value < 9)) {}

if i let my browser execute this codeblock i get this error message via mozilla firebug:

"TypeError: aPlayGround[(Actual - 1)] is undefined"

bBool is my error handler to make sure that the second condition in line 11 doesn't get checked but he still does it works if i change last line to:

if (false && (aPlayGround[Actual-1].Value < 9)) {}

im actually working on a very ugly workaround that just increases the amount of elements to just bypass the error but there must be some way more elegant way to make the browser ignoring this undefined-error unfortunately im a very beginner in js so please keep it simple thx


回答1:


Don't use new Boolean, simply cast the result to a boolean using this:

var bBool = !!(Actual%Width);


来源:https://stackoverflow.com/questions/17590180/javascript-override-undefined

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