• anguments


    anguments是一个对象,长得很像数组的对象,但不是数组,而是伪数组

    arguments的内容是函数运行时的实参列表

    (function(d, e, f) {
        console.log(arguments);       //["hello","world","!","aa"]
        console.log(arguments[3]);    //aa
    })('hello', 'world', '!','aa');

    传进匿名函数的参数只有前三个,实际上却能从arguments里获取到第四个。

     

    ----------------------------------------------------分割线----------------------------------------------------

    arguments的特点:

    1、传进函数的实参本来只能通过形参传递,但是js里可以通过arguments传递

    2、如果实参个数比形参要多,arguments仍然可以获取到,并且可以修改形参的值,修改形参数值的时候,形参也是做相应变化的。变化是双向的。

    3、有2个常用属性,length获取长度,callee获取当前所在函数

    (function(d, e, f) {
        console.log(arguments); //["hello","world","!","aa"]
        arguments[0]='china';
        console.log(d);         //["chia","world","!","aa"]
        e='s';
        console.log(arguments);   //["china","s","!","aa"]
        var length=arguments.length;
        console.log(length);         //4
        console.log(arguments.callee);   //答案在下面 详见:答一
    })('hello', 'world', '!','aa');

    答一

     function (d, e, f) {
        console.log(arguments);
        arguments[0]='china';
        console.log(d);
            e='s';
            console.log(arguments);
        var length=arguments.length;
        console.log(length);
        console.log(arguments.callee);
    }

    ----------------------------------------------------分割线----------------------------------------------------

    使用callee计算1到n的和的函数

    console.log((function(n){
        if(n==1){
            return 1;
        }else{
            return n+arguments.callee(n-1);      //10+9+8+7+6+5+4+2+1=55
        }
    }(10)));
  • 相关阅读:
    git命令
    WPF让绑定支持表达式
    WPF多语言动态切换的一种方法
    C#监测系统事件,睡眠状态
    记一次渗透测试
    Relay
    ECShop相关漏洞复现/分析
    人工智能学习路线图
    抽奖算法
    关于微信开发的 appid,openid,unionid
  • 原文地址:https://www.cnblogs.com/coldfishdt/p/6532955.html
Copyright © 2020-2023  润新知