Home  >  Article  >  Web Front-end  >  Does JavaScript have named functions?

Does JavaScript have named functions?

WBOY
WBOYOriginal
2022-04-11 14:55:531620browse

There are named functions in JavaScript. You can use the function keyword to declare a named function. The syntax is "function function name (parameter list) {statement block to be executed;}"; the named function can be called before and after the named function declaration. When the function declaration is promoted, it will be automatically promoted to the top. .

Does JavaScript have named functions?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

Does JavaScript have named functions?

JavaScript has named functions

  function 函数名(参数列表){//function声明函数的关键字
     要执行的语句块;
   }

function: Keywords that define functions (must be lowercase)

Function name: A custom name (the function must be called in the same case as the function name)

Function body: A certain piece of code to be encapsulated, which can complete a specific function

Features: Function declaration promotion, automatically promoted to the top

Function declaration can be called anytime and anywhere. Named functions can be called before and after, anonymous functions can only appear after being called after the function.

The JavaScript code taking unit is a function. The function can contain a piece of executable code, and can also accept parameters passed in by the caller. There are mainly the following ways to define functions in JavaScript:

The first way: named function

<script>
  function 函数名(参数列表){//function声明函数的关键字
     要执行的语句块;
   }
   function myAge(age) {
            document.write(&#39;我的年龄是&#39; + age)//我的年龄是19 
        }
        myAge(19)//函数不调用,不执行
</script>

The second way: anonymous function

<script>
    function (参数列表) {//同引用函数调用这个函数
        要执行的语句块
     }
     //匿名函数
      var a=function(age) {
          document.write(&#39;我的年龄是&#39; + age)//我的年龄是19
       }
       a(19)
    </script>

Named functions are different from anonymous functions. Named functions can be called before and after, while anonymous functions can only appear after being called after the function.

[Related recommendations: javascript video tutorial,webfrontend

The above is the detailed content of Does JavaScript have named functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is es6 class a function?Next article:Is es6 class a function?