JS——Function对象

1、对象的功能
Function对象比较的特殊,它就相当于Java中的方法;

function fun(){
            alert("hi function!");
        }
            alert(fun);
            alert(fun.toString());
            fun();

下面显示前两个的结果。
这里写图片描述
2、对象的创建

var fun1 = function(a,b){
    alert("hi function");
};

var fun2 = new Function("alert('hello');");

3、对象的属性

alert(fun1.length);  代表fun1函数的参数个数。

4、对象的方法
toString打印函数的定义

5、函数的调用
js中的函数在调用时,只看函数名称.不看参数.

<html>
 <head>
    <title>Function对象</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        function fun(a,b){
            alert(a+b);
        }
    fun(1,2);       //3
    fun(1,2,3);     //3
    fun(1,2,3,4);   //3 
    fun();          //NaN
</script>
  </head>
  <body>
  </body>
</html>

6、arguments是函数运行时的参数的封装

<html>
  <head>
    <title>Function对象</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
    function argu(){
        var count = 0;
        for(var i=0;i<arguments.length;i++){
            count+=arguments[i];
        }
        alert(count);
    }
    argu(1,2);      //3
    argu(1,2,3);    //6
    argu(1,2,3,4);  //10
</script>
  </head> 
  <body>  
  </body>
</html>

7、Function中的return关键字
与java一样,JS中也使用return关键字;当然return也可以结束方法的调用;

function fun1(a,b){
        alert(a+b);
        return a+b;
    }
    alert(fun1(1,2));

输出:3
3

function fun2(){
        alert("haha");
        return ;
        alert("heihei");
    }
    alert(fun2());//undefined

这里return阻止了下面heiehi的运行。haha运行完了以后运行undefied;

8、void运算符: 用于拦截方法的返回值.

<html>
  <head>
    <title>Function对象</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
    function fun1(a,b){
        alert(a+b);
        return a+b;
    }
</script>
  </head> 
  <body>
    <a href="javaScript:void(fun1(1,2));" >点我</a><br>
     <a href="javaScript:void(0)" onclick="alert('haha')"  >点我</a>
  </body>
</html>

上面的点我执行跳出3;下面就直接haha

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值