Pre*_*cho 0 javascript typescript
在打字稿中编码时遇到了一个奇怪的问题.不知何故2> 100 == true(?).
我真的没弄清楚......
这是我的代码:
if (!this.multipleYAxis) {
for (let d of this.getDatasources()) {
let options = this.getGraphTypeOption(d.id);
console.log(this.yMax + ' < ' + options.max);
console.log(this.yMax < options.max);
if (this.yMax < options.max)
this.yMax = options.max;
if (this.yMin > options.min)
this.yMin = options.min;
}
if (this.getChart().yAxis[1] != undefined) {
this.getChart().yAxis[1].update({
max: this.yMax
});
this.getChart().yAxis[1].update({
min: this.yMin
});
}
}
Run Code Online (Sandbox Code Playgroud)
yMin和yMax声明如下:
private yMin: number = 0;
private yMax: number = 0;
Run Code Online (Sandbox Code Playgroud)
options声明如下:
export interface GraphTypeOption {
...
max: number;
min: number;
...
}
Run Code Online (Sandbox Code Playgroud)
我目前正在测试的代码正在运行2个数据源.因此for循环将运行两次.
这是我的输出:
100 < 100
false
100 < 2
true
Run Code Online (Sandbox Code Playgroud)
在chrome开发人员工具控制台中,我可以将100 <2变为true的唯一方法是键入"100"<"2",但正如您在我的声明中所看到的那样this.yMax,options.max显然是一种int/number.编译器甚至不喜欢我想明确地将它们转换为int,因为强制转换函数需要string类型的变量.
谁知道是什么导致了这个麻烦?是打字机 - > javascript搞砸了吗?
正如你在我的声明中看到的那样,this.yMax和options.max显然是一种int/number
然而,如果你看到的100 < 2是true,那么你正在比较字符串,尽管有类型注释.使用调试器,检查您要比较的内容的值.你会发现,他们"100"和"2",不100和2.然后,您可以找到它们是字符串的根本原因.
无端的例子:
console.log("100" < "2"); // true
console.log(100 < 2); // falseRun Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |