In javascript, the function must be like this: {code ...} if it is reversed, it will not work, for example: {code ...} but in php, you can: {code ...} why can't javascript be declared after a call in the same scripting language? In subsequent versions of javascript, do you want to implement such functions in... javascript as follows:
function a(){}a();
If it is reversed, it will not work, for example:
a();function a(){};
But in php, we can do this:
a();function a(){}
Why can't javascript be declared after a call in the same scripting language? Will this function be implemented in subsequent javascript versions?
Reply content:
In javascript, the function must be as follows:
function a(){}a();
If it is reversed, it will not work, for example:
a();function a(){};
But in php, we can do this:
a();function a(){}
Why can't javascript be declared after a call in the same scripting language? Will this function be implemented in subsequent javascript versions?
function func(){...}This isFunction DeclarationBefore the code is executed, it is fully loaded to the frontend Of the scope. Therefore, you can post it in code writing (in the same scope or upper scope, that is, the subject calls the question before declaring it.
- Function declaration/variable declaration will be
hoistingWhere the function declaration will be complete and the variable DeclarationOnly the pre-declaration partWhileWithout pre-assignment.
Code:
(function(){ f1() f2() var f1 = function(){} function f2(){}})()
Runtime:
(Function () {var f1, function f2 () {}// hoisting, declared by implicit elevation f1 () // ReferenceError: f1 is not defined f2 () f1 = function (){}})()
Conclusion:
Currently, js supports calling the subject before declaring it.
Http://jsfiddle.net/ZvLht/
At present, ES5 is completely no problem, but it seems that this situation in ES6 will change, the impression should be in the http://es6.ruanyifeng.com/here to see, but do not remember where it is, you can flip it over.
For improvementhoistingYou must strongly recommend @ nihtire's blog post:
Understanding JavaScript (2)
@ Nihtire the predecessors explained and introduced in detail in the blogScoping & HoistingThe subject will be clear after reading it patiently.
By the way, I also introduced a bit of relevant content in my blog post. You may wish to read it as well. :)
Javascript Functions (1): function declaration and function expressions
Are you sure you want?
test();function test() { console.log('test string');}
After you really learn functional programming and understand the closures in js, you will understand that js functions are much easier to use than php functions.
This should be a unified standard language that is compatible with various browsers.
How did the author come to this conclusion.
Alert (());
Function (){
Return 1;
} // 1
Such code can be run.
Alert (());
Var a = function (){
Return 1;
} // Error: unexpected indentifier
This code will report an error.
The function declaration will take the lead in parsing the js parser to ensure that the function is available before other code is executed. The function expression must be interpreted and executed only when the Code parser parses the code to its line.
Are you sure you tried your code?
The landlord's problem is wrong.
You have no idea about how to upgrade the function declaration of JS.
a();function a(){};
This can be executed, but not as follows
A (); // a is undefined, and a is already declared, because var a, but var a = function () {} is not assigned (){};