Home >Web Front-end >Front-end Q&A >What are the custom functions of javascript?

What are the custom functions of javascript?

青灯夜游
青灯夜游Original
2022-03-28 20:07:032381browse

Custom functions include: 1. Named function, syntax "function function name(){}"; 2. Anonymous function, syntax "var variable name=function(){};"; 3. Object function , the syntax is "var variable name=Function();"; 4. Self-tuning function, which implements the definition and call of the function together.

What are the custom functions of javascript?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

A function is a set of delayed actions that can be triggered by events or called in other scripts.

Two types of functions in JavaScript: predefined functions and custom functions

  • Predefined functions

    • [parseInt / parseFloat】string to int/float

    • ##【isNaN / isFinite】Detect whether the parameter is a non-numeric value/infinite number

    • 【escape / unescape 】Encode/decode string

    • 【eval】Execute JavaScript script

    • 【alert / confirm / prompt】three warning boxes

  • Custom function

    • [Named function]

      function funcName(){}

    • 【Anonymous function】

      var x=function(){};

    • ##【Object function】
    • var x= Function();

    • 【Self-adjusting function】
    • (function(){}());

##Custom functions

In addition to predefined functions, you can also use custom functions in JavaScript.

When customizing a function, there is no need to declare the parameter type of the function, nor the return type of the function.

JavaScript currently supports the following customization methods:

Named function
  • Anonymous function
  • Object function
  • Self-tuning function
  • 1. Named function

Named functions are defined by the function keyword, followed by the function name and parentheses ().

    After completing the definition of the function, the function will not be executed automatically, but will only be executed when called through an event or script.
  • In the same 3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0 tag, the function call can be before or after the function definition.
  • In different 3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0 tags, the definition of the function must precede the call of the function, otherwise the call will be invalid.
  • The format is as follows:
function funcName([parameters]){
  statements;
  [return 表达式;]
}

2. Anonymous function

The definition format of anonymous functions is basically the same as that of named functions, except No name of the function is provided, and there should be a semicolon; at the end. Since there is no function name, you need to use variables to accept anonymous functions to facilitate subsequent function calls.

    Named functions are easy for beginners to get started, but their readability is poor. Anonymous functions are relatively more convenient to use and have better readability. Currently, most popular JavaScript frameworks basically use anonymous functions to define functions.
  • The format is as follows:
var x=function([parameters]){
  statements;
  [return 表达式;]
};

3. Object function

JavaScript provides the Function class for defining functions. The format is as follows:

var func1=new Function([parameters],statements;);

Function is a keyword used to define a function, and the first letter must be capitalized.

    parameters are function parameters, optional. When there are multiple parameters, separate them with commas.
  • statements is the function execution body. When there are multiple execution statements, the statements are separated by semicolon;.
  • 4. Self-calling function

Generally, the function itself will not be executed automatically and will only be executed when called. Therefore, JavaScript provides a self-calling function that implements function definition and call together. The format is as follows:

(function([parameters]){
  statements;
  [return 表达式;]
})([params]);

You need to use parentheses () to enclose the self-tuning function and end with a semicolon;

    parameters are formal parameters, optional. Parameters are separated by commas.
  • params are actual parameters, data is passed in when the function is called.
  • Code example:
  • <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <title>test</title>
    </head>
    <body>
      <input id="btn1" type="button" onclick="button1Event()" value="button1" />
      <input id="btn2" type="button" onclick="button2Event()" value="button2" /><br />
      <input id="a" type="text"/><input id="b" type="text"/><br />
      <input id="sumBtn" type="button" value="x+y" onclick="sum()"/>
      
      <script type="text/javascript">
        function button1Event(){/* 命名函数 */
            alert("命名函数:按钮1被点击!");
        }
        var x=function(){/* 匿名函数 */
            alert("匿名函数:按钮2被点击!");
        }
        button2Event=x;
        
        var y=new Function("a", "b", "return a+b");/* 对象函数 */
        sum=function(){
            var aValue=parseInt(document.getElementById("a").value,10);
            var bValue=parseInt(document.getElementById("b").value,10);
            alert("对象函数:"+y(aValue,bValue));
        }
        
        var tema=2;var temb=2;
        (function(a,b){/* 自调函数 */
            alert("自调函数:"+(a+b));
        })(tema,temb);
      </script>
    </body>
    </html>
Effect demonstration:

##【Related recommendations:

javascript video tutorial,

web front-end

The above is the detailed content of What are the custom functions of javascript?. 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