Home > Article > Web Front-end > What are the custom functions of javascript?
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.
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
function funcName(){}
var x=function(){};
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 functionNamed functions are defined by the function keyword, followed by the function name and parentheses ().
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.
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.
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;
<!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-endThe 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!