artikel/javascript/oomodell
Kurt Z
- javascript
0 molily
HI
folgendes Beispiel aus http://aktuell.de.selfhtml.org/artikel/javascript/oomodell/index.htm#aboutmodl
gibt in meinem FF2.0.0.11 _zwo_mal_ undefined aus, das ist nicht beabsichtigt oder? Liegt das an der JS Version?
function x(z) {
alert(x.z);
}
x("calling x");
alert(x.z);
Gruß
kurt
Hallo,
der Artikel hat Fehler. Mehrere. Du hast einen gefunden.
»Wird eine Funktion ausgeführt, so wird für jeden formalen Parameter (in JavaScript müssen Parameter nicht zwingend formal deklariert werden) das Attribut im Funktions-Objekt auf den Wert des entsprechenden Parameters gesetzt. Ist die Funktion jedoch zu Ende gelaufen, wird das Attribut wieder auf undefined gesetzt:«
Das ist halt einfach falsch.
Was stimmt:
Wird eine Funktion ausgeführt, so wird für jeden formalen Parameter das Attribut im VARIABLEN-Objekt der Funktion auf den Wert des entsprechenden Parameters gesetzt.
Das Variablen-Objekt ist ein internes Objekt, darauf kann man nicht zugreifen.
ECMAScript dazu:
»Every execution context has associated with it a variable object. Variables and functions declared in the source text are added as properties of the variable object. For function code, parameters are added as properties of the variable object.«
Und jetzt kommt der Satz, der ins Deutsche mit Fehler übertragen wurde:
»for each formal parameter, as defined in the FormalParameterList, create a property of the variable object whose name is the Identifier and whose attributes are determined by the type of code.«
variable object, nicht function object o.ä.!
(
Ausnahme für Code außerhalb von Funktionen:
»Variable instantiation is performed using the global object as the variable object and using property attributes { DontDelete }.«
http://bclary.com/log/2004/11/07/#a-10.2.1
)
Das variable object in Funktionen geht auf das activation object zurück:
»Variable instantiation is performed using the activation object as the variable object and using property attributes { DontDelete }.«
http://bclary.com/log/2004/11/07/#a-10.2.3
»The activation object is purely a specification mechanism. It is impossible for an ECMAScript program to access the activation object. It can access members of the activation object, but not the activation object itself.«
http://bclary.com/log/2004/11/07/#a-10.1.6
Ich weiß nicht, wie Christian auf etwas anderes gekommen ist...
Mathias