最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript String Comparison Oddity with String.toLowerCase - Stack Overflow

programmeradmin1浏览0评论

while curious (and no JS background) I'm beginning to dive into Typescript and face a brick wall. I want to pare two strings and to make life easy they will be aligned to lowercase first. This is the code:

let bool: boolean = false;
let i = 0;
thisparisons[++i] = " init bool " + " => " + bool;

bool = false;
if ("a" == "a") { bool = true };
thisparisons[++i] = ' "a" == "a" ' + " => " + bool;

bool = false;
if ("a" == "b") { bool = true };
thisparisons[++i] = ' "a" == "b" ' + " => " + bool;

bool = false;
if ("a" == "A") { bool = true };
thisparisons[++i] = ' "a" == "A" ' + " => " + bool;

bool = false;
if ("a".toLowerCase == "A".toLowerCase) { bool = true };
thisparisons[++i] = ' "a".toLowerCase == "A".toLowerCase ' + " => " + bool;

bool = false;
if ("a".toLowerCase == "B".toLowerCase) { bool = true };
thisparisons[++i] = ' "a".toLowerCase == "B".toLowerCase ' + " => " + bool;

and it prints:

init bool => false

"a" == "a" => true

"a" == "b" => false

"a" == "A" => false

"a".toLowerCase == "A".toLowerCase => true

"a".toLowerCase == "B".toLowerCase => true

Why does the last expression evaluate to true?

"a" == "b" should evaluate to false like the third statement.

while curious (and no JS background) I'm beginning to dive into Typescript and face a brick wall. I want to pare two strings and to make life easy they will be aligned to lowercase first. This is the code:

let bool: boolean = false;
let i = 0;
this.parisons[++i] = " init bool " + " => " + bool;

bool = false;
if ("a" == "a") { bool = true };
this.parisons[++i] = ' "a" == "a" ' + " => " + bool;

bool = false;
if ("a" == "b") { bool = true };
this.parisons[++i] = ' "a" == "b" ' + " => " + bool;

bool = false;
if ("a" == "A") { bool = true };
this.parisons[++i] = ' "a" == "A" ' + " => " + bool;

bool = false;
if ("a".toLowerCase == "A".toLowerCase) { bool = true };
this.parisons[++i] = ' "a".toLowerCase == "A".toLowerCase ' + " => " + bool;

bool = false;
if ("a".toLowerCase == "B".toLowerCase) { bool = true };
this.parisons[++i] = ' "a".toLowerCase == "B".toLowerCase ' + " => " + bool;

and it prints:

init bool => false

"a" == "a" => true

"a" == "b" => false

"a" == "A" => false

"a".toLowerCase == "A".toLowerCase => true

"a".toLowerCase == "B".toLowerCase => true

Why does the last expression evaluate to true?

"a" == "b" should evaluate to false like the third statement.

Share Improve this question asked Apr 19, 2016 at 20:00 Robert NordenRobert Norden 1001 gold badge1 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

To call a method you must use the parentheses (), even when there are no arguments to pass to the method:

bool = false;
if ("a".toLowerCase() == "B".toLowerCase()) { bool = true };

Or simply:

bool = ("a".toLowerCase() == "B".toLowerCase());

Without the parentheses, "a".toLowerCase is simply a reference to the String.toLowerCase method itself. The result of the parison is true because it pares the two methods and finds that they are indeed the same method.

发布评论

评论列表(0)

  1. 暂无评论