Bureaucrats, emailconfirmed
1,221
edits
(Created page with "==Variablen-Hoisting==") |
No edit summary |
||
Line 1: | Line 1: | ||
==Variablen-Hoisting== | ==Variablen-Hoisting== | ||
''Hoisting'' ist das "hochziehen" von Variablen an den Funktionsanfang; so als wären Variablen die irgendwo in der Funktion deklariert worden, eigentlich Anfang der Funktion deklariert. Der Javascript-Interpreter macht dies automatisch. Von diesem Verhalten ist allerdings nur die Deklaration betroffen nicht die Zuweisung eines Wertes zu der Variablen. | |||
d.h., dass aus einem Code, der so geschrieben wurde: | |||
<source lang="javascript"> | |||
function doCalc(){ | |||
var aNumber = 4; | |||
console.log("Die Nummer ist:"+aNumber); | |||
var anotherNumber = aNumber*2; | |||
console.log("Die andere Nummer ist:"+anotherNumber); | |||
} | |||
</source> | |||
Bei der Ausführung vom Interpreter so "umgeschreiben" wird: | |||
<source lang="javascript" highlight="2,3"> | |||
function doCalc(){ | |||
var aNumber; | |||
var anotherNumber; | |||
aNumber = 4; | |||
console.log("Die Nummer ist:"+aNumber); | |||
anotherNumber = aNumber*2; | |||
console.log("Die andere Nummer ist:"+anotherNumber); | |||
} | |||
</source> |