dedlfix: Cast: String oder new String

Beitrag lesen

Tach!

Ob jeder falsy-Wert zu 0 oder '' gewandelt werden soll, ist auch offen. Noch schlimmer: nicht jeder truthy-Wert ist true. Ganz böses Beispiel:

Boolean.prototype.identifyYourself = function(t) {
    console.log("I am " + (this ? 'true' : 'false'));
};
false.identifyYourself();

// I am true

Nicht alles was ein Programmierfehler ist, ist eine gültiger Aussage. So wäre es richtig:

Boolean.prototype.identifyYourself = function() {
    console.log("I am " + (this.valueOf() ? 'true' : 'false'));
};
false.identifyYourself();

Wenn an einem primitiven Typ eine Objekt-Methode aufgerufen werden soll, wird der Wert in ein Objekt konvertiert. Objekte sind nie falsy, weswegen der einfache boolsche Test nicht richtig ist. Stattdessen muss man den Wert testen, nicht das Objekt selbst.

dedlfix.

0 69

Cast: String oder new String

pl
  • javascript
  1. 0
    dedlfix
    1. 0
      pl
    2. 0
      Rolf B
      1. 0
        pl
        1. 1
          dedlfix
          1. 0
            pl
            1. 0
              dedlfix
              1. 0
                pl
          2. 0
            Rolf B
            1. 0
              dedlfix
              1. 0
                Rolf B
                1. 1
                  dedlfix
                2. 0
                  1unitedpower
        2. 0
          1unitedpower
          • javascript
          • typescript
          1. 0
            pl
  2. 0
    ursus contionabundo
    1. 0
      dedlfix
      1. 0
        ursus contionabundo
    2. 0
      pl
      1. 0
        ursus contionabundo
        1. 0
          pl
          1. 0
            ursus contionabundo
            1. 0
              pl
              1. 0

                Ganz unverblümt...

                ursus contionabundo
                • meinung
                1. 0
                  pl
                  1. 0
                    ursus contionabundo
                    1. -2
                      pl
                      1. -1
                        ursus contionabundo
                        1. 0
                          Felix Riesterer
                          1. 0

                            Frage immer noch offen!

                            pl
                            1. 0
                              dedlfix
                              1. 0
                                pl
                                • javascript
                                1. 0
                                  Felix Riesterer
                                  1. 0
                                    pl
                                2. 1
                                  dedlfix
                                  1. 0
                                    pl
                                    1. 0
                                      dedlfix
                                      1. 0
                                        pl
                          2. -1
                            pl
                            1. 0
                              Felix Riesterer
                              1. -1
                                pl
                                • zu diesem forum
                                1. 0
                                  Gunnar Bittersmann
                                  • meinung
                                  1. 1
                                    dedlfix
                                    1. 0
                                      Christian Kruse
                                      1. 0
                                        dedlfix
                                        1. 0
                                          Tabellenkalk
                                          1. 0
                                            Gunnar Bittersmann
                                            • logik
                                        2. 0
                                          Gunnar Bittersmann
                                          • logik
                                          1. 0
                                            dedlfix
                                            • meinung
                                            1. 0
                                              MudGuard
                                            2. 0
                                              Gunnar Bittersmann
                                      2. 1
                                        JürgenB
                                        1. 0
                                          Felix Riesterer
                                          1. 0
                                            Gunnar Bittersmann
                                    2. 0
                                      Gunnar Bittersmann
                                      1. 0
                                        Tabellenkalk
                                        1. 0
                                          Gunnar Bittersmann
                                          1. 0
                                            Rolf B
                                    3. 0
                                      Orlok
                                      • mathematik
                                  2. 0
                                    Gunnar Bittersmann
                                    • musik
                            2. 0
                              Gunnar Bittersmann
                              • meinung
                              • zu diesem forum
                            3. 0
                              ursus contionabundo
                              1. 0

                                (Ergänzung)

                                ursus contionabundo
                                1. 0
                                  Rolf B
                                  1. 0
                                    ursus contionabundo
                                2. 0
                                  Rolf B
                                  • meinung
                                  • performance
                                  1. 0
                                    ursus contionabundo
                                    1. 0
                                      Rolf B
                                      • humor
                                      • meinung