With objects especially, I don't understand what parts of the object run before initialization, what runs at initialization and what runs sometime after.
EDIT: It seems that parsetime is the wrong word. I guess I should have formulated the question "In the 2-pass read, what gets read the first pass and what gets read the second pass?"
With objects especially, I don't understand what parts of the object run before initialization, what runs at initialization and what runs sometime after.
EDIT: It seems that parsetime is the wrong word. I guess I should have formulated the question "In the 2-pass read, what gets read the first pass and what gets read the second pass?"
Share Improve this question edited Oct 29, 2016 at 17:24 Piper 1,2733 gold badges15 silver badges26 bronze badges asked Oct 26, 2010 at 12:38 randomablerandomable 4171 gold badge6 silver badges7 bronze badges 5- It depends on your code. – SLaks Commented Oct 26, 2010 at 12:41
- 1 When you ask about Javascript, are you specifically referring to it being used within a web browser environment? – Spudley Commented Oct 26, 2010 at 12:43
- 1 @Spudley, I didn't know it could be used in any other environment, so yes. – randomable Commented Oct 26, 2010 at 12:47
- 1 there are server-side JS implementations, plus it's also built into some other apps as scripting language. – Spudley Commented Oct 26, 2010 at 12:52
- Great question, but I'll have too look elsewhere for a good answer. – Emanegux Commented Jun 28, 2013 at 15:15
5 Answers
Reset to default 39A javascript file is run in a 2-pass read. The first pass parses syntax and collects function definitions, and the second pass actually executes the code. This can be seen by noting that the following code works:
foo();
function foo() {
return 5;
}
but the following doesn't
foo(); // ReferenceError: foo is not defined
foo = function() {
return 5;
}
However, this isn't really useful to know, as there isn't any execution in the first pass. You can't make use of this feature to change your logic at all.
Unlike C++, it is not possible to run logic in the Javascript parser.
I suspect that you're asking which code runs immediately and which code runs when you create each object instance.
The answer is that any code in a function that you call will only run when you call the function, whereas any code outside of a function will run immediately.
Not sure what you ask exactly so I'll just share what I know.
JavaScript functions are "pre loaded" and stored in the browser's memory which means that when you have function declared in the very end of the page and code calling it in the very beginning, it will work.
Note that global variables, meaning any variable assigned outside of a function, are not preloaded, so can be used only after being declared.
All commands outside of a function will be parsed in the order they appear.
JavaScript doesn't really have "runtime", it can only respond to events or have code executed via global timers. Any other code will be parsed and "forgotten".
While JavaScript's direct ancestor is Scheme, JavaScript didn't inherit macros, so the answer is fairly simple: there is never any code run during parse time.
Roughly speaking, Interpreter gets all variables and functions first, and then they get hoisted and executed.
For more detail, I hope these links might be helpful:
- http://adripofjavascript.com/blog/drips/variable-and-function-hoisting
- https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/