zoukankan      html  css  js  c++  java
  • callee, caller,toString(),String()

     1. 函数内部有两个特殊对象: argumentsthis.

       arguments 有一个属性 :callee. 用 callee 实现函数的递归。该属性是一个指针,指向拥有这个arguments 对象的函数

      普通的函数递归:

     1   function factories(num){
     2         if(num <= 1){
     3             return 1;
     4         }else{
     5             return num * factories(num-1);
     6         }
     7     }
     8     var an = factories;
     9     console.log(factories(4));    //24
    10     console.log(an(4));            //24

      此时,若有一个变量同样指向factories, 当factories 改变 ,  另一个变量也随之变化,如下:

     1   function factories(num){
     2         if(num <= 1){
     3             return 1;
     4         }else{
     5             return num * factories(num-1);
     6         }
     7     }
     8     console.log(factories(4));    //1
     9 
    10     var an = factories;
    11 
    12     factories = function(){
    13         console.log(1);
    14     }
    15 
    16     console.log(an(4));        //重写了factories, an 也随之变了,结果是 1

    解决方法如下, 就是 arguments.callee 存在的价值

      function factories(num){
            if(num <= 1){
                 return 1;
             }else{
                 return num * arguments.callee(num-1);
             }
        }
         
        var an = factories;
     
        factories = function (){
            return 0;
        }
        console.log(factories(4));    //0
        console.log(an(4));//24

      另外顺便记录下 caller <本身的调用者>:

      function outer(){
            alert(inner());
        }
    
        function inner(){
            return arguments.callee.caller;
        }
    
        outer();

       上面代码返回的是 function outer(){...}

    2. 打算写的东西还有很多,但是晚上的时间真的很有限

      再简单记录下 toString() 跟 String()

      首先,w3c上面查询了下,找到

      http://www.w3school.com.cn/tiy/t.asp?f=jseg_tostring_boolean 是讲的boolean.toString()

      几乎所有的object 都可以使用 toString()方法,除了 Null 跟 Undefined 类型!但是使用 String(NUll) 跟 String(Undefined); 可以转换成字符串

      

    疯癫不成狂,有酒勿可尝;世间良辰美,终成水墨白。
  • 相关阅读:
    subprocess
    bytes(str_, encoding="utf8")
    按文件生成时间 排序 批量与生成同步上传文件
    async
    http trigger 事件源是事件的生产者,函数是事件的处理者
    分片上传
    使用 FFmpeg 处理高质量 GIF 图片
    兴趣 主题 字段 二值化 多值并列属性 拆分 二值化
    打开 回收站
    shell如何查看单个或多个文件的行数或总行数
  • 原文地址:https://www.cnblogs.com/chuyu/p/3155854.html
Copyright © 2011-2022 走看看