The Javascript tutorial I follow always defines before using, but my PHP book always defines at the end, and in fact points out that this is considered good practice.
Is there a reason to do it one way or the other?
The Javascript tutorial I follow always defines before using, but my PHP book always defines at the end, and in fact points out that this is considered good practice.
Is there a reason to do it one way or the other?
Share Improve this question asked Apr 26, 2012 at 0:13 iDontKnowBetteriDontKnowBetter 4531 gold badge7 silver badges19 bronze badges 2- 4 Javascript and PHP are very different languages. It might well be that "good practices" in one language can't be transferred to the other. In this case, however, I think at least in PHP it's not good practice to mix class definitions with top-level code at all. You can easily use separate files here. – Niklas B. Commented Apr 26, 2012 at 0:16
- 1 what do you mean "define classes and functions". show us a code sample. in most programming language that i know of, you usually define members first, then a constructor, then methods (or functions). – botbot Commented Apr 26, 2012 at 0:21
3 Answers
Reset to default 5If you have inline code executing (e.g. executing as it is loaded), then global variables must be defined before the code that uses them.
Functions can be defined in whatever order you think makes the code the most tidy and easiest to read.
For example, in this code:
foo();
function foo() {
alert(x);
}
var x = 4;
The call to foo()
will alert undefined
because x does not yet have a value when foo()
is called, but you will notice that foo
can be called in code that appears before the function definition because all functions are loaded before any code is actually executed.
As for a best practice, I think it makes sense to organize your code in the best way you can find that puts modules of related functionality together, but the order is not generally material. I trust you realize that javascript doesn't have anything that is actually a class. It can use function objects and prototypes to simulate some class-like behavior that other languages have, but it doesn't really have classes as it's objects are based on prototypes, not classes.
In Javascript is mon practice to declare everything inside a closure, could be an object, or a function, so you don't pollute the global scope. When defining varaibles is often remended to declare them at the top of the function to which they're scoped.
Javascript doesn't have classes, but defining object literals or constructor functions can be done anywhere as long as you keep in mind that function declarations are not the same as function expressions:
function foo () { ... } // Declaration, works anywhere
var foo = function () { ... } // Expression, works only after the assignment
More info here http://javascriptweblog.wordpress./2010/07/06/function-declarations-vs-function-expressions/
In Javascript, a function must be defined before to use it. So the function definition must precede the code using the function.
Some remendations for enhancing page speed tell us to put the Javascript execution at the end of the HTML page. Doing so, the Javascript will start when the page and all elements are quite fully loaded. (see article here)
Nevertheless, the remaining usage for Javascript is to place the function declaration in the <header>
section, so that the defined functions are immediately operational in any element of the body, including the <body>
element itself. You can always place the bootstraps or other function execution at the end of the HTML page.
For PHP it is different.
In a PHP script, you can define the function anywhere in the code, even after the code that is using the functions. That is possible because PHP has to read the full script before the run the very first line of code.
Furthermore, for readability of the code, the remaining usage for PHP is to start with the algorithm, and to place the function definitions at the end of the script (or more often, to place the functions a a separated script).