2016-04-19 17 views
0

好奇心が強い(そしてJSの背景はありません)私はTypescriptに潜入し始め、レンガの壁に直面しています。 2つの文字列を比較したいと思います。簡単にするには、小文字に揃えるようにしてください。これはコードです:Typescript文字列の比較String.toLowerCaseでの奇妙な扱い

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 

最後の式が真と評価されないのはなぜ?

"a" == "b"は3番目のステートメントと同様にfalseと評価されます。 "a".toLowerCaseは、括弧なしで

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

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

それとも単に:あなたはメソッドに渡す引数がない場合でも、括弧()を使用しなければならないメソッドを呼び出すために

答えて

2

単にString.toLowerCaseメソッド自体への参照です。比較の結果はtrueです。なぜなら、2つの方法を比較して、実際に同じ方法であることがわかるからです。

+0

よく、すぐに感謝! :) –

+0

将来の読者のためのもう1つのヒント:Visual Studioコードでコーディングする場合、2016-04-20バージョン1.0.0の必須の '()'を追加するのではなく**自動補完に注意してください。 –