Calocybe: Was bedeutet dieser Code?

Beitrag lesen

Hi!

Das überprüft, ob variable "keinen" Wert hat (und nicht ob sie existiert, im Mozilla wird bei undefinierten Variabeln in dem Falle auch eine Warnung ausgegeben).

Die Bedingung ergibt auch dann true, wenn variable nicht definiert ist, egal ob Mozilla da eine Warnung gibt oder nicht.

Der ausdruck ist wahr, wenn:

variable = "";
variable = 0;
variable = null;
variable = false;

Aus ECMA-262-3:
12.5. The if Statement
  [...]
  The production IfStatement : if ( Expression ) Statement is evaluated as follows:
  1. Evaluate Expression.
  2. Call GetValue(Result(1)).
  3. Call ToBoolean(Result(2)).
  4. If Result(3) is false, return (normal, empty, empty).
  5. Evaluate Statement.
  6. Return Result(5).

9.2 ToBoolean
  The operator ToBoolean converts its argument to a value of type Boolean
  according to the following table:
  Input Type | Result
  -----------+-------------
  Undefined  | false
  Null       | false
  Boolean    | The result equals the input argument (no conversion).
  Number     | The result is false if the argument is +0, 0, or NaN;
             | otherwise the result is true.
  String     | The result is false if the argument is the empty string (its length is zero);
             | otherwise the result is true.
  Object     | true

D.h. Du hast weitgehend recht, Du hast nur noch NaN und undefined vergessen.

So long

--
"Microsoft ist über die Probleme informiert, hat aber noch nicht reagiert."
"Der Patch %s, der das Problem %s beheben sollte, funktioniert offenbar nicht."
"Wann ein Update verfügbar sein wird, ist nicht bekannt."
    (Textbausteine der Heise-Newsticker-Redaktion)