Typescript字符串与String.toLowerCase的比较奇数
Typescript String Comparison Oddity with String.toLowerCase
虽然很好奇(没有JS背景),但我开始深入Typescript,并面临着一堵砖墙。我想比较两个字符串,并使生活更容易,他们将首先对齐小写字母。这是代码:
let bool: boolean = false;
let i = 0;
this.comparisons[++i] = " init bool " + " => " + bool;
bool = false;
if ("a" == "a") { bool = true };
this.comparisons[++i] = ' "a" == "a" ' + " => " + bool;
bool = false;
if ("a" == "b") { bool = true };
this.comparisons[++i] = ' "a" == "b" ' + " => " + bool;
bool = false;
if ("a" == "A") { bool = true };
this.comparisons[++i] = ' "a" == "A" ' + " => " + bool;
bool = false;
if ("a".toLowerCase == "A".toLowerCase) { bool = true };
this.comparisons[++i] = ' "a".toLowerCase == "A".toLowerCase ' + " => " + bool;
bool = false;
if ("a".toLowerCase == "B".toLowerCase) { bool = true };
this.comparisons[++i] = ' "a".toLowerCase == "B".toLowerCase ' + " => " + bool;
并打印:
init bool => false
"a" == "a" => true
"a" == "b" => false
"a" == "A" => false
"a".toLowerCase == "A".toLowerCase => true
"a".toLowerCase == "B".toLowerCase => true
为什么最后一个表达式的求值结果为true?
"a"=="b"应该像第三条语句一样计算为false。
要调用方法,必须使用括号()
,即使没有要传递给方法的参数:
bool = false;
if ("a".toLowerCase() == "B".toLowerCase()) { bool = true };
或者简单地说:
bool = ("a".toLowerCase() == "B".toLowerCase());
如果没有括号,"a".toLowerCase
只是对String.toLowerCase
方法本身的引用。比较的结果是true
,因为它比较了两种方法,发现它们确实是相同的方法。
相关文章:
- 比较从函数和生成的日期对象
- 如何使用 node.js 比较两个 json 数组
- jQuery自定义验证比较多个输入的序列
- 反向字符串比较
- 通过JSON&比较时间
- 将DOM节点值与字符串Javascript进行比较
- 从键值结构中获取数据,并将其与AngularJS中ng重复的值进行比较
- 递归深度比较
- 可以't在JavaScript中比较两个字符串
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- 如何在moment.js中只比较日期
- 无法比较javascript变量
- 比较javascript中的浮动并更改最高浮动的颜色
- 使用javascript比较对象中的对象值和数组长度
- 如何将smarty变量与javascript变量进行比较
- 比较两个数组中的元素
- MongoDB-通过比较集合和对象的数组来返回现有字段的数组
- 比较和转换多个数组
- 如何在JavaScript中比较两个日期
- Typescript字符串与String.toLowerCase的比较奇数