SoFunction
Updated on 2025-02-28

Detailed explanation of Function type in javascript

Function Type

The function type is undoubtedly a very important thing in js.

1. This thing is first of all, which means it is a reference type. Statement: When I heard it was an object, does it have a very illusion of its base class that is the object object object illusion, No,

It and object are two separate things. When you typeof function, the return is funciton and not object

2. Each function is an instance of the Function object, which has properties and methods like other reference objects. Since it is an object, the function name is a pointer to the function object

Syntax support for function declarations:

<script>

  //Method 1  function fun(num1,num2){
    return num1+num2;
  }
  
  //Method 2  var fun=function(num1,num2){
    return num1+num2;
  };

  //Method 3  var fun=new Function("n1","n2","return n1+n2");
  
</script>

Explanation: All three ways can complete the declaration of a function, but each has its own differences.

Method 1 is understood as a declaration of a function, method 2 and method 3 is understood as a function expression. (It is not recommended to use Method 3, because the reason will cause the code to be parsed twice. First, explain the regular ECMAScript code and explain the passed parameters.

How to write, the parameters can be N, but the last parameter is regarded as the body of the function)

Why is the difference mainly lies in the difference between the js parser's parser's parsing function declarations and function expressions. The parser will read the function declaration first, and the js engine will automatically place the function declaration at the top of the execution environment when executing.

Function expressions are different. Only when they are executed to the function expression will they be truly interpreted and executed. It is very important to understand this!

Look at the code

<script>
  (typeof fun); //"function"
  (typeof fun2); //"undefined"
  (typeof fun3); //"undefined"
  function fun(n1,n2){
   return n1+n2;
  }
  var fun2=function(n1,n2){
   return n1+n2;
  }
  var fun3=new Function("n1","n2","return n1+n2;");
 </script>

3. Why is the function not overloaded?

This problem needs to be considered from the language characteristics of js. As stated in Article 2, the function name is just a pointer to the function object. It will be clear to understand based on the concept of pointer.

See the code example:

&lt;script&gt;
  function fun(n1){
   return n1+100;
  }
  function fun(n1){
   return n1+200;
  }
  (fun(1));// 201 
  
  //Above writing should be after analysis  
  var fun=function(n1){
    return n1+100;
  }
  fun=function(n1){
   return n1+200;
  }
  
  // Fun's reference is overwritten  (fun(1));// 201 
 &lt;/script&gt;

That’s all I’ve written about today, and I hope it will help those who have blind spots in this area! If you have any questions to correct me, I hope you will leave a domineering message to correct me.

The above is the entire content of this article, I hope you like it.