zoukankan      html  css  js  c++  java
  • 函数的callee和caller

    callee是函数的arguments这个特殊对象的一个属性,它会指向这个arguments对象的函数:

    function fn1(){
        console.log(arguments.callee);        //输出函数fn1本身
    }
    fn1()
    callee的可以消除函数内部调用自己的耦合性,
     
    var num = 3
    function fn1(num){
        if(num <= 1){
            return 1
        }else{
            return num * fn1(num-1)
        }
    }
    //可以使用callee
    function fn1(num){
        if(num <= 1){
            return 1
        }else{
            return num * arguments.callee(num-1)
        }
    }

    还可以使用callee查看形参与实参的个数差别

    function fn2(a,b,c){
        console.log('实参数:',arguments.length);            //2
        console.log('行参数:',arguments.callee.length);       //3
    }
    fn2(1,2)
     
    函数对象还有一个caller属性,该属性指向调用当前函数的引用,也就是调用当前函数的函数。但如果要是在全局中调用当前函数,caller的值就是null:
    function fn1(){
        console.log(fn1.caller);       //fn1的caller属性指的是函数fn2
        console.log(arguments,callee.caller)    //同上,使用arguments的callee属性,指的是当前函数,当前函数的caller属性,指的是其调用者,也就是函数fn2
    }
    function fn2(){
    }
    fn2()
  • 相关阅读:
    python 集合
    jQuery选择器
    hdu 5747 Aaronson
    hdu 2049 不容易系列之(4)——考新郎
    hdu 2048 神、上帝以及老天爷
    hdu 2045 不容易系列之(3)—— LELE的RPG难题
    hdu 2047 阿牛的EOF牛肉串
    hdu 2046 骨牌铺方格
    hdu 2050 折线分割平面
    hdu 2044 一只小蜜蜂
  • 原文地址:https://www.cnblogs.com/CZforever/p/10853637.html
Copyright © 2011-2022 走看看