There are two methods for declaring functions in JS, namely:
function() { // Some code}; function// Some code}
Why are there two different ways to do it? What are the advantages and disadvantages of each method? Is there a way to do it and another way to do it?
by @Greg
The difference is that Functionone is only actually defined on the line where the assignment is reached, and functiontwo is defined immediately when the function or script that contains it executes. For example:
1 <script>2//Error undefined3Functionone ();45var functionone =function() {6}; 8 // No error Functionone (); 11 12 <script>13 // No error14 Functiontwo (); 15 16 function Functiontwo () {17 }18 </script>
This also means that you cannot define a function in a conditional statement using the second method:
<script> if (test) { // Error or Misbehavior function Functionthree () { DoSomething (); }}</script>
The above example, regardless of whether the value of test is true or FALSE, has actually defined the Functionthree function.
by @Eugene Lazutkin
Alternatively, you can combine the two functions to define the method:
Function abc () {};
The XYZ function will be defined normally. The ABC function is not defined in the browser (except IE), but is visible in its function body:
function ABC () { // xyz is visible here // ABC was visible here}// xyz is visible Here// abc are undefined here
If you want to use a function alias in all browsers, you can use this declarative way:
function ABC () {}; var xyz = ABC;
In this case, XYZ and ABC are aliases for the same function object:
prints "true"
One convincing reason for a function definition in the second way is that the function object has a "name" attribute (IE is not supported ). When you define a function similar to the following:
function// prints "abc"
The Name property of the function is automatically assigned. But when you define a function in the following way:
function// prints ""
Its Name property is empty--this creates an anonymous function and assigns it to other variables.
Another reason to recommend using the second function definition method is that you can refer to the function itself with a short internal name, while providing an external user with a long, conflict-free alias:
assume really.long.external.scoped is {}really. function shortcut (n) { //Let it call itself recursively: Loop calls itself shortcut (n-1// /Let is pass itself as a callback: as a callback function to pass the parameter // ...}
In the example above we can use an external declaration to do the same thing as the original function, but it is inconvenient (and too slow).
(The other way to refer to the function itself is to use Arguments.callee, which is more common < does not know that translation is correct, and is not supported in strict mode).
In fact, JavaScript treats two methods of declaring a function differently. This is a function declaration:
Function abc () {}
The ABC here is defined everywhere in the current scope:
// Works// Yet, it is defined down there. function ABC () {}// ///Works
Again, it is defined in advance through a return statement:
// Worksreturn; Function abc () {}
Another function expression:
function () {};
Here xyz is defined in the Declaration line:
// UNDEFINED!!! //function() {}/ ////Works
For function expressions and function declarations, I prefer the Declaration of Function expressions (the first way) because I can control visibility in this way. When I define such a function:
function () {};
I know I've defined a local function. When I define a function like this:
function () {};
I know I've defined a global function, and I don't define abc< anywhere in the scope chain, and I'm not sure how to translate >. However, the following definitions are as follows:
Function abc () {};
The difference between the two function declarations of var functionname = function () {} and function functionname () {} in JS