当前位置: 代码网 > it编程>编程语言>Javascript > JavaScript自定义函数用法详解

JavaScript自定义函数用法详解

2024年05月19日 Javascript 我要评论
javascript中的函数分为两种:系统函数和自定义函数,这里主要讲解自定义函数。自定义函数1、语法:注意:传入的参数是可选的。例如:<!doctype html><html la

javascript中的函数分为两种:系统函数和自定义函数,这里主要讲解自定义函数。

自定义函数

1、语法:

注意:

  • 传入的参数是可选的。

例如:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>自定义函数</title>
    <script>
        // 语法1自定义无参函数
       function custom(){
           document.write("自定义无参函数,使用第一种语法定义"+"<br />")
       };
       // 语法2
       var customer=function(){
        document.write("自定义无参函数,使用第二种语法定义"+"<br />")
       };
       // 定义有参函数
       function customwithpara(i){
           document.write("自定义有参函数,使用第一种语法定义,i的值是:"+i+"<br />")
       };
       // 语法2
       var customerwithpara=function(i){
           document.write("自定义有参函数,使用第二种语法定义,i的值是:"+i+"<br />")
       };
    </script>
</head>
<body>
    
</body>
</html>

2、函数的调用

函数可以通过函数名加上括号中的参数进行调用。

例如:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>函数调用</title>
</head>
<body>
    <script>
       // 定义无参函数
       function custom(){
           document.write("这是无参的函数"+"<br />");
       };
       // 定义无参的函数变量
       var customer=function(){
        document.write("这是无参的函数变量"+"<br />");
       };
       // 定义有参函数
       function customwithpara(para){
           document.write("这是有参函数,参数值是:"+para+"<br />");
       }
       // 定义有参的函数变量
       var customerwithpara =function(para){
           document.write("这是有参的函数变量,参数值是:"+para+"<br />");
       }

       // 函数调用
       // 1、调用无参函数
       custom();
       // 2、调用有参函数
       customwithpara(45);
       // 无参函数变量的调用
       customer();
       // 有参函数变量的调用
       customerwithpara(23);
    </script>
</body>
</html>

结果:

注意:

  • 调用函数时需要注意函数调用的顺序。如果是自定义函数,那么也可以在函数定义之前调用函数,因为这时会自动把函数的定义放到最前面。如果是通过变量的形式定义函数,那么必须先定义函数才能调用。

看下面的例子:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>函数调用</title>
</head>
<body>
    <script>
       // 函数调用
       // 1、调用无参函数
       custom();
       // 2、调用有参函数
       customwithpara(45);
       // 无参函数变量的调用
       customer();
       // 有参函数变量的调用
       customerwithpara(23);

       // 定义无参函数
       function custom(){
           document.write("这是无参的函数"+"<br />");
       };
       // 定义无参的函数变量
       var customer=function(){
        document.write("这是无参的函数变量"+"<br />");
       };
       // 定义有参函数
       function customwithpara(para){
           document.write("这是有参函数,参数值是:"+para+"<br />");
       }
       // 定义有参的函数变量
       var customerwithpara =function(para){
           document.write("这是有参的函数变量,参数值是:"+para+"<br />");
       }


    </script>
</body>
</html>

结果:

3、匿名函数

匿名函数:顾名思义,即没有函数名称的函数。其语法如下图所示:

例如:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>匿名函数</title>
</head>
<body>
    <script>
        // 传统定义函数的方式
        function fn(){
            document.write("这是传统函数的定义"+"<br />");
        };
        // 调用
        fn();
        // 匿名函数的定义和调用
        (function(){
            document.write("这是匿名函数"+"<br />");
        })();
    </script>
    
    
</body>
</html>

结果:

4、匿名函数的应用

匿名函数可以作为函数的参数进行调用,看下面的例子:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>匿名函数的应用</title>
    <script>
         // 匿名函数应用
         function fun(para){
            document.write("参数的值是:"+para+"<br />");
        };
        // 用匿名函数作为函数的参数
        fun(function(){
            return 5;
        }());

        // 也可以使用下面的方式
        function fu(para){
            document.write("参数的值是:"+para()+"<br />");
        };
        fu(function(){
            return "56";
        });
    </script>
</head>
<body>
    
</body>
</html>

结果:

到此这篇关于javascript自定义函数用法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com