views:

61

answers:

4

I'm completely muddled up about what code runs at runtime and what code runs at parsetime. With objects especially, I don't understand what parts of the object run before initialization, what runs at initialization and what runs sometime after.

Since I'm completely confused, I'm not sure how complicated this question is. If it's not just a simple rule or two that answers the question, is there perhaps an article or cheat sheet somewhere? Google is giving me squat.

EDIT: It seems that parsetime is the wrong word. Gareth understood what I meant. 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?" Does that question make more sense?

+2  A: 

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.

SLaks
Is there any way to make a function in an object that runs when the object initializes?
randomable
@Randomable: What kind of object? You can call the function in the constructor.
SLaks
Okay, so you have to call it? There is no syntax of creating a function that makes it automatically run? I think you inadvertently answered my question just then.
randomable
+2  A: 

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.

Jörg W Mittag
Don't you mean "Self", not "Scheme"? That's where it got the prototypal inheritance from. It really didn't get that much from Scheme.
Pointy
+3  A: 

A 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.

Gareth
It's very useful to know, because I could easily have written the code in your second example and not understood why it didn't work. Also, I didn't realize there was no execution during the first pass, so you're definitely clearing things up for me.
randomable
+1  A: 

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.

JavaScript global variables, meaning any variable assigned outside of a function, are also preloaded and can be used by code anywhere in the page.

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".

Shadow Wizard