Javascript 自定义编号。原型使用变量,但不直接使用编号
我已在我的JS中包括custom Number.prototype,如下所示:Javascript 自定义编号。原型使用变量,但不直接使用编号,javascript,jquery,Javascript,Jquery,我已在我的JS中包括custom Number.prototype,如下所示: Number.prototype.isBetween = function (first, last) { return (first < last ? this >= first && this <= last : this >= last && this <= first); }; 结果: 真的 但当我尝试执行以下操作时,它抛出了一个错误: 4
Number.prototype.isBetween = function (first, last) {
return (first < last ? this >= first && this <= last : this >= last && this <= first);
};
结果:
真的
但当我尝试执行以下操作时,它抛出了一个错误:
40.isBetween(10,50)
结果:
未捕获的语法错误:无效或意外标记
如何使这个(40.isBetween(10,50))起作用?你可以用括号把数字括起来解决这个问题
(40).isBetween(10,50)
// => true
如果没有括号,语法中就会出现歧义,语言解析器会故意抛出错误来避免这种歧义
有关详细信息和其他解决方案,请参阅此问题的答案:40..isBetween。。。。数字方法区别于小数点的特殊性:p或偶数(40)。介于。。。或者40.0.介于…我不明白你的意思?你能再解释一下吗?
被解释为小数点。由于40.
与40.0
相同,40.isBetween
基本上是指40.0isBetween
。Super.-)工作。。!!!
(40).isBetween(10,50)
// => true