Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How JavaScript customizes functions

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "JavaScript how to customize the function", the editor shows you the operation process through the actual case, the operation method is simple and fast, practical, I hope this "JavaScript how to customize the function" article can help you solve the problem.

There are two kinds of functions in JavaScript: system functions and custom functions. Here we mainly talk about custom functions.

Custom function 1. Syntax:

Note:

The parameters passed in are optional.

For example:

Custom function / / Syntax 1 Custom no-parameter function function custom () {[xss_clean] ("Custom no-parameter function, define with the first syntax" + ")}; / / Syntax 2 var customer=function () {[xss_clean] (" Custom no-parameter function, define "+" using the second syntax)} / / define the parameterized function function customWithPara (I) {[xss_clean] ("Custom parameterized function, defined using the first syntax, the value of I is:" + I + "")} / / Syntax 2 var customerWithPara=function (I) {[xss_clean] ("Custom parameterized function, defined in the second syntax, the value of I is:" + I + "")}; 2. Function call

The function can be called by adding parameters in parentheses to the function name.

For example:

Function call / / define function custom () {[xss_clean] ("this is a function with no parameters" + ");}; / / define a function variable var customer=function () {[xss_clean] (" this is a function variable with no parameters "+");} / / define the parameter function function customWithPara (para) {[xss_clean] ("this is a parameter function, the parameter value is:" + para+ ");} / / define the function variable var customerWithPara = function (para) {[xss_clean] (" this is a function variable with parameters, the parameter value is: "+ function") } / / function call / / 1, call non-parameter function custom (); / / 2, call parameter function customWithPara (45); / / call customer () of no parameter function variable; / / call customerWithPara of parameter function variable (23)

Results:

Note:

You need to pay attention to the order of function calls when calling functions. If it is a custom function, you can also call the function before the function definition, because the definition of the function is automatically put first. If a function is defined in the form of a variable, the function must be defined before it can be called.

Look at the following example:

Function call / / function call / / 1, call no parameter function custom (); / / 2, call parameter function customWithPara (45); / / call customer () of no parameter function variable; / / call customerWithPara of parameter function variable (23) / / define function custom () {[xss_clean] ("this is a function with no parameters" + ");}; / / define a function variable var customer=function () {[xss_clean] (" this is a function variable with no parameters "+");} / / define the parameterized function function customWithPara (para) {[xss_clean] ("this is a parameterized function, the parameter value is:" + para+ ");} / / define the parameterized function variable var customerWithPara = function (para) {[xss_clean] (" this is a parameterized function variable, the parameter value is: "+ para+");}

Results:

3. Anonymous function

Anonymous function: as the name implies, a function that does not have a function name. The syntax is shown in the following figure:

For example:

Anonymous function / / traditional way of defining a function function fn () {[xss_clean] ("this is the definition of a traditional function" + ");}; / / call fn () / / definition and call of anonymous function (function () {[xss_clean] ("this is an anonymous function" + ");}) ()

Results:

4. Application of anonymous functions.

Anonymous functions can be called as arguments to the function, as shown in the following example:

Application of anonymous functions / / anonymous functions apply function fun (para) {[xss_clean] ("the value of the parameter is:" + para+ ");}; / / use the anonymous function as the parameter fun (function () {return 5;} ()) / / you can also use the following method function fu (para) {[xss_clean] ("the value of the parameter is:" + para () + ");}; fu (function () {return" 56 ";})

Results:

This is the end of the introduction to "how to customize functions in JavaScript". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report