JavaScript中是否有内置方法来检查数字是否介于两个之间?我使用if(x < i && x > l)
,但希望有更好的解决方案。
就像Felix Kling在另一篇文章中所说的那样,您将无法更快或更好地完成它。
但是,如果您经常使用它,则可以创建一个原型方法:
Number.prototype.inRange = function (a, b) {
return this >= a && this <= b;
};
var num = 8;
console.log(num.inRange(7, 8));// true
num = 4;
console.log(num.inRange(7, 8));// false
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句