简体   繁体   English

强制进行不正确的比较以在JavaScript中引发警告/错误

[英]Force improper comparisons to throw a warning/error in JavaScript

I had JavaScript code similar to the following: 我有类似以下的JavaScript代码:

var data = {"d": 12345};
for(var i = 0; i < data.length; ++i) {
    console.log(i);
}

I know that this for loop will not execute any cycles because data.length will be 'undefined' and 我知道这个for循环不会执行任何循环,因为data.length将是'undefined'和

0 < 'undefined' //false (I think this is false for any int?)

Anyways, while I fixed my code (in my code, data contains an array but I was accidentally referencing data.length instead of data.myarr.length) I'm hoping to avoid such problems in the future - took me quite some time to find this bug. 无论如何,当我修复我的代码时(在我的代码中,数据包含一个数组,但我不小心引用了data.length而不是data.myarr.length)我希望将来避免这样的问题 - 花了我一段时间发现这个bug。

I have been reading up on strict mode in JavaScript but after messing around with JSFiddle I couldn't get strict mode to throw the above as an error. 我一直在阅读JavaScript中的严格模式,但在搞乱了JSFiddle后,我无法通过严格模式将上述内容作为错误抛出。 I tried both making the entire script strict, then, thinking maybe there was a conflict, tried it within its own method and still got nothing. 我试过让整个脚本严格,然后,想想可能存在冲突,在自己的方法中尝试并且仍然没有任何结果。

Is there a way to force "nonsensical" comparisons such as int < "undefined" to throw an error (or even a warning)? 有没有办法强制“无意义”的比较,如int <“undefined”抛出错误(甚至是警告)? Did I miss something with strict mode? 我是否错过了严格的模式? Or is the language design supposed to allow such things? 或者语言设计应该允许这样的事情?

There is nothing within Javascript itself that will pick this one up. Javascript本身没有任何内容可以选择这个。

I haven't kept track of the latest developments in JS Lint , but I don't know of anything there that would help. 我没有跟踪JS Lint的最新发展,但我不知道那里会有什么帮助。

On the other hand, TypeScript handles this situation very well. 另一方面, TypeScript可以很好地处理这种情况。

Just as head's up -- this isn't something that can change in Javascript without breaking existing code. 正如头脑一样 - 这不是可以在不破坏现有代码的情况下改变Javascript的东西。 There are enough array like objects that have a both a length property and numericly named property names that one iterates over. 有足够多的array like对象具有一个length属性和一个迭代的数字命名的属性名称。

  • The arguments keyword arguments关键字
  • Node lists 节点列表

AFAIK, there's no real way to achieve what you're after. AFAIK,没有真正的方法来实现你所追求的目标。 since int < undefined is a perfectly fine comparison. 因为int < undefined是一个非常精细的比较。

One thing you could do, for this specific issue, is use Array.forEach ( MDN ). 对于此特定问题,您可以做的一件事是使用Array.forEach( MDN )。 This would throw an error since data.forEach is undefined and attempting to call that as a function will fail ( unless you added a "forEach" key on data with a function value, of course ): 这会引发错误,因为data.forEach undefined并且尝试将其作为函数调用将失败( 除非您在具有函数值的数据上添加了“forEach”键,当然 ):

var data = {"d": 12345};
data.forEach(function(item, index, array){
    console.log(index);
});

Note, that IE < 9 does not have Array.forEach, so you would need to add it to Array's prototype. 注意,IE <9没有Array.forEach,因此您需要将它添加到Array的原型中。 See more here, including how to do so, at Array.prototype.forEach() on MDN . 在MDN上的Array.prototype.forEach()中查看更多内容,包括如何执行此操作。

In ES6, you could use an iterator via for...of , which would result in an error since non-array objects are not iterable: 在ES6中,您可以使用迭代器来for...of ,这会导致错误,因为非数组对象不可迭代:

for (var x of data) { }

You will have make other arrangements if you also want the index. 如果您还想要索引,您将做出其他安排。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM