JSLint doesn’t expect my tildes

大城市里の小女人 提交于 2019-12-12 09:35:11

问题


JSLint insists that my use of the somewhat exotic tilde operator in the below example is unexpected. What I’m wondering is whether this is a limitation of JSLint? or strict mode? or what else am I missing?

(function () {
    'use strict';
    if (~'foo'.indexOf('bar')) {
        return 'wild accusations';
    }
}());

Also, why shouldn’t I use the simple-looking tilde operator instead of the more complex example below? Surely there must be a good reason not to?

if ('foo'.indexOf('bar') >= 0) { … }

回答1:


From the JSLint Docs:

Bitwise Operators

JavaScript does not have an integer type, but it does have bitwise operators. The bitwise operators convert their operands from floating point to integers and back, so they are not as efficient as in C or other languages. They are rarely useful in browser applications. The similarity to the logical operators can mask some programming errors. The bitwise option allows the use of these operators: << >> >>> ~ & |.

You can enable it under options

Cheers



来源:https://stackoverflow.com/questions/9473746/jslint-doesn-t-expect-my-tildes

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