Timeline for Javascript function scoping and hoisting
Current License: CC BY-SA 3.0
16 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 31, 2021 at 7:38 | comment | added | Sebastian Simon |
Rereading my previous comment, I’ll add a minor correction: even in var a = function(){};, the function won’t be anonymous, because that statement is evaluated via NamedEvaluation, so the function’s name will be a, implicitly. It still makes a difference, though: inside named function expressions, as in var a = function a(){...};, an immutable binding to a is created; not so in var a = function(){...};.
|
|
| Jul 30, 2021 at 15:42 | comment | added | Guerric P | Downvoted because it contains a wrong statement as pointed out by @SebastianSimon. Only declarations are hoisted, not assignments | |
| Oct 26, 2020 at 21:21 | comment | added | supercat |
@PeterOlson: I wonder if the behavior of function hoisting changed to accommodate them. If a block contains a let which gets closed over by a function, a separate function object will need to be created each time the block is executed. In the absence of let, it would generally make more sense to create one function object when the outer function is executed.
|
|
| Dec 28, 2019 at 15:34 | comment | added | Peter Olson |
@SurajJain This answer was written in 2011, quite a while before let and const came into common use.
|
|
| Dec 28, 2019 at 14:08 | comment | added | Suraj Jain | @PeterOlson I understand your lie to children comment, but this answer is at top, and we now have let and const, if someone reads this they do not get complete understanding and fail to understand how let and const works, you can have a rigorous definition below your original answer, it will help others. | |
| Nov 4, 2019 at 19:00 | comment | added | Shubham Jain | Function hoisting doesn't means that functions are moved to the top of their scope instead, In the creation phase of the Execution Context when the parser runs through the code it sets up memory space for variables and functions (which is called Hoisting). Its not actually moving code to the top of the page, but before your code begins to be executed line by line, the JS engine has set aside the memory space, so when the code begins to execute, those things are actually already sitting into the memory.and therefore we have access to them in a limited way. | |
| Dec 17, 2016 at 13:48 | comment | added | xgqfrms | Function hoisting > var hoisting | |
| Aug 18, 2016 at 19:22 | comment | added | Sebastian Simon |
"Also, in this instance, function a() {} behaved the same as var a = function () {};" — this is incorrect in two ways: first, if anything, it would’ve been var a = function a() {}; (the function is actually not anonymous), second, those two forms are not interchangeable, because from var a = function a() {}; only the var a; part would’ve been hoisted. The a = function a() {}; part would still have been behind the return statement. Because the original form is a function declaration and not a function expression, it actually gets hoisted as a whole.
|
|
| Jul 30, 2015 at 1:11 | comment | added | Peter Olson | @RobG Sure, I guess you could call the description a small "lie to children", but in the end the behavior is the same, whether the code is literally rearranged or just the order of execution is rearranged. What actually happens behind the scenes is more of an academic concern, and might even be implementation-dependent. | |
| Jul 30, 2015 at 0:22 | comment | added | RobG |
In no way is the function body "rewritten". The various ECMAScript standards clearly state that variable and function declarations are processed before code execution begins. That is, nothing is moved, it's about order of execution (hence my dislike of the term "hoisting", which infers movement or rearrangement). In your re–written code, the declaration var a should be before the function declaration, and the assignment a = 1 should be after. But note that this isn't specified to actually happen by the parser, tokeniser, interpreter, compiler, whatever, it's just an equivalent.
|
|
| Nov 10, 2011 at 20:11 | history | edited | Peter Olson | CC BY-SA 3.0 |
deleted 13 characters in body
|
| Sep 21, 2011 at 21:44 | comment | added | Peter Olson | @dev.e.loper Yes, in Javascript, functions are first class objects, just like strings and numbers. That means they are defined as variables and can be passed to other functions, be stored in arrays, and so on. | |
| Sep 21, 2011 at 21:41 | comment | added | dev.e.loper | So all the function declarations are eventually assigned to a variable? | |
| Sep 21, 2011 at 21:41 | history | edited | Peter Olson | CC BY-SA 3.0 |
added 13 characters in body
|
| Sep 21, 2011 at 21:38 | vote | accept | dev.e.loper | ||
| Sep 21, 2011 at 21:33 | history | answered | Peter Olson | CC BY-SA 3.0 |