这行是什么意思“ operator.indexOf(btnVal) > -1”

what does this line mean " operators.indexOf(btnVal) > -1"?

本文关键字:btnVal 是什么 意思 indexOf operator      更新时间:2023-09-26

我正在研究这个网站的javascript计算器 http://thecodeplayer.com/walkthrough/javascript-css3-calculator但是我被困在代码中operator.indexOf(btnVal)> -1这句话是什么意思?

operators要么

是字符串,要么是数组。 indexOf在字符串/数组中查找查询的子字符串/值的索引。

String.Prototype.indexOf

Array.Prototype.indexOf

如果未找到子字符串/值,则返回-1> -1是对相反情况的检查,即子字符串/值存在于字符串/数组中。

例子:

console.log('xyz'.indexOf('z')); // 2
console.log('xyz'.indexOf('y')); // 1
console.log('xyz'.indexOf('yz')); // 1
console.log('xyz'.indexOf('q')); // -1
console.log([1, 'abc', true].indexOf('abc')); // 1
console.log([1, 'abc', true].indexOf('true')); // -1
console.log([1, 'abc', true].indexOf(true)); // 2
console.log([1, 'abc', true].indexOf('a')); // -1
console.log([1, 'abc', true].indexOf(1)); // 0
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

检查

btnVal以保存一些已知operators的值(如"+","-"等)

比 operator.indexOf(btnVal)> -1 读作"这是一个已知的运算符吗?"