zoukankan      html  css  js  c++  java
  • 函数

    应用递归  将 从x到x递减 (每次减1) 累乘值赋给f

    var f = function fact(x) { if (x < 1) return 1; else return x * fact(x - 1);};
    /*函数名称将成为函数内部的一个变量*/

    对象属性里函数里的this的指向

    var o = {                                  //对象o
        m:function() {                         //对象中的方法m()
            var self = this;                   //将this的值保存至一个变量中
            console.log(this === o);           //输出true , this就是对象o
            f();                               //调用辅助函数f()
    
            function f() {                     //定义一个嵌套函数f()
                console.log(this === o);       //"false"; this 的值是全局对象或undefined
                console.log(self === o);       //"true"; self的指外部函数的this
            }
        }
    }

    函数实参 arguments

    function(x,y,z) {
        console.log(arguments.length);          // =>3   实参个数
        console.log(arguments[1]);              // =>y   实参y  
        arguments[2] = null                     // 修改实参元素的值
        console.log(z);                         //输出"null"  如果y是数组的话,输出就不是"null"
    }
    
    //利用arguments 模拟Max.max()  求最大值
    function() {
        var max = Number.NEGATIVE_INFINITY;  //遍历实参并查找记住最大值
        for (var i = 0; i < arguments.length; i++){
            if (arguments[i] > max){
                max = arguments[i];
            }
            return max;
        }
    }
    
    var largest = max (1, 10, 100, 2, 3, 1000);  //=> 1000

    callee和caller 属性

    //callee和caller在ECMAScript5严格模式下不支持
    //caller 是非标准的, 它指代调用当前正在执行的函数的函数
    //callee 属性指代当前正在执行的函数
    
    //在匿名函数中通过callee来递归
    var factorial = function(x) {
        if (x <= 1) return 1;
        return x * arguments.callee(x - 1);
    }

     sum 求和函数

    //判断是不是数组
    var isArray = Function.isArray || function(o) {
        return typeof o === "object" &&
        Object.prototype.toString.call(o) === "[object Array]";
    };
    
    //判断是不是伪数组
    
    function isArrayLike(o){
        if(o &&                                       //o非null.undefined
            typeof o === "object" &&                  //o是对象
            isFinite(o.length) &&                     //o.length是有限数值
            o.length > 0 &&                           //o.length为非负数
            o.length === Math.floor(o.length) &&      //o.length是整数
            o.length < 4294967296 ){                  //o.length<2^32         
            return true;
        }else{
            return false;
        }
    }
    //sun 求和函数
    
    function sum(a) {
        if (isArrayLike(a)) {                                                           //必须是数组
            var total = 0;
            for (var i = 0; i < a.length; i++) {                                        //遍历所有元素
                var element = a[i];
                if (element == null) {continue;}                                        //跳过null和undefined
                if (isFinite(element)) {                                                //必须是合法数字
                    total += element;
                }else{
                    throw new Error("sum(): elemnts must be finite numbers");
                }
            }
            return total;
        }else{
            throw new Error("sum(): argument must be array-like");
        }
    
    }
    
    
    function flexisum(a){
        var total = 0;
        for (var i = 0; i < arguments.length; i++) {                                    //遍历所有实参
            var element = arguments[i], n;
            if (element == null) {continue;}                                            //忽略null和undefined
            if (isArray(element)) {                                                     //如果实参是数组
                n = flexisum.apply(this,element);                                       //递归计算累加和
            } else if (typeof element === "function") {                                 //如果是函数
                n = Number(element());                                                  //调用做类型转换
            } else {
                n = Number(element);                                                    //否则直接做类型转换
            }
            if (isNaN(n)) {                                                             //判断是否为非法数字
                throw Error("flexisum(): can't convert " + element +" to number");       
            } else {
                total += n;
            }
        }
        return total;
    }
  • 相关阅读:
    团队绩效评估计划
    Beta阶段项目总结
    Alpha阶段项目总结
    Alpha版总结会议
    快上车项目简介(500字)
    第二阶段团队冲刺站立会议10
    第二阶段团队冲刺站立会议09
    第二阶段团队冲刺站立会议08
    第二阶段团队冲刺站立会议07
    第二阶段团队冲刺站立会议06
  • 原文地址:https://www.cnblogs.com/ysxq/p/6781296.html
Copyright © 2011-2022 走看看