javascript覆盖未定义 [英] javascript override undefined

查看:77
本文介绍了javascript覆盖未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索了许多小时后,我没有找到解决问题的方法:

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)) {}

如果我让我的浏览器执行此代码块,则会通过mozilla firebug获得此错误消息:

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

TypeError:aPlayGround [ ((实际-1)]未定义

bBool 是我的错误处理程序,确保第11行中的第二个条件没有得到检查,但是如果我将最后一行更改为:$ p $ b,他仍然可以运行

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实际上是在一个非常丑陋的解决方法上工作,它只是增加了元素数量以绕过错误,但是不幸的是,必须有一种更优雅的方法使浏览器忽略此未定义错误
我是JS的初学者,所以请保持简单
thx

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

推荐答案

请勿使用 new布尔,只需使用以下命令将结果强制转换为布尔值:

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

var bBool = !!(Actual%Width);

这篇关于javascript覆盖未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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