SoFunction
Updated on 2025-02-28

Detailed explanation of JavaScript reference type Function instance

This article describes the JavaScript reference type Function. Share it for your reference, as follows:

Function Type

Functions are objects, and function names are pointers: each function is an instance of type Function. Since a function is an object, the function name is a pointer to a function object and will not be bound to a certain function. A function may have multiple names.

function getSum(a, b) {
  return a + b;
}
alert(getSum(2, 3));//5
var copy = getSum;
alert(copy(2, 3));//5
getSum = null;
alert(copy(2, 3));//5

There are three ways to define functions:Function declarationFunction expressionsFunction constructor

The Function constructor receives any number of parameters. The previous parameters enumerate the parameters of the function, and the last parameter is the function body. It is generally not recommended to use Function constructor to define functions.

var getSum = new Function("a", "b", "return a + b";

JavaScript does not have the concept of function overloading. If two functions with the same name are declared, the following functions will overwrite the previous function.

Since the function name itself is a variable, the function can also be used as a value. Not only can a function be passed as a parameter, but also as a return value.

function callFunction(func, arguments) {
  return func(arguments);
}
function sayHello(name) {
  return "Hello, " + name;
}
var result = callFunction(sayHello, "Alice");
alert(result);//Hello, Alice

①, function declaration

function function name (parameters|optional) { function body }

example:

function func1(){// Declaration, part of the program  function func() {// Declare part of the function body    ...//Function body  }
}

②, Function expression

function function name|optional(parameter|optional) { function body}

Example 1:

var fun = function func() {};//Expression, part of the assignment expression

Example 2:

new function func(){};//Expression, new expression

Example 3:

(function func(){});
//Expression, contained in grouping operator - bracket(), only expressions can be contained in bracket()

Example 4:

[function func() {}];//Expression, only expressions can be expressed in the array initializer

Example 5:

1, function func() {};//Expression, commas can only operate expressions

③, function constructor

The [[Scope]] property of a function created by the function constructor contains only global objects.

<script>
  var a = 1;
  func1();
  function func1() {
    var a = 2;
    var b = 3;
    var func2 = new Function("alert(a); alert(b);");
    func2();//1,b is not defined
  }
</script>

The function func2 created by the function constructor can get the variable a from the global object, but there is no b in the global object, so an error is reported: b is not defined.

The difference between function declaration and function expression

1) Function declarations are created when entering the context stage and are available in the code execution stage. Function expressions are created in the code execution stage, so function declarations can be improved, but function expressions cannot.

Function declaration promotion:

func();//Function declaration
function func(){
  alert("Function declaration");
}

Function expressions cannot be promoted:

func();//An error: func is not a functionvar func = function (){
  alert("Function expression");
}

2) Function declaration affects the variable object VO, that is, the variable object stored in the VO in the context. The function expression does not affect the variable object VO and does not exist in the variable object. This means that it is neither possible to call it before the function declaration by name nor after the declaration. However, function expressions can call themselves by name in recursive calls.

Example 1:

alert(func); //An error: func is not a function.  The definition is not available before, because it is created during the code execution phase(function func() {});
alert(func); //An error: func is not a function.  It is also not available after definition because it is not in the variable object VO

Example 2:

(function func(param) {
  if (param) {
    return;
  }
  func(true);//Func is available, recursive calls can be called by name})();
func(); // func is not a function, externally unavailable

3) Function declarations can only appear in the program or function body, and cannot appear in expressions or blocks ({ … }), such as if, while or for statements. Because JavaScript has no block-level scope, only functions and global scopes. Function expressions appear at the position of the expression.

Function declaration: Some browsers will return if, while others will return else.

if (true) {
  function func() {
    alert('if');
  }
}
else {
  function func() {
    alert('else');
  }
}
func();

Function expression: All browsers return if.

var func;
if (true) {
  func = function() {
    alert('if');
  };
}
else {
  func = function() {
    alert('else');
  };
}
func();

For more information about JavaScript, readers who are interested in reading this site's special topic:JavaScript object-oriented tutorial》、《Summary of common JavaScript functions techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.