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 };
それとも単に:あなたはメソッドに渡す引数がない場合でも、括弧()
を使用しなければならないメソッドを呼び出すために
よく、すぐに感謝! :) –
将来の読者のためのもう1つのヒント:Visual Studioコードでコーディングする場合、2016-04-20バージョン1.0.0の必須の '()'を追加するのではなく**自動補完に注意してください。 –