zoukankan      html  css  js  c++  java
  • JavaScript中的arguments,callee,caller,call,appy

    摘自JavaScript中的arguments,callee,caller,call,appy(博客园用的少,专门为引用源链接花了些时间看了看怎么引,尊重原程序员的辛苦啊)

    演示arguments的用法,如何获取实参数和形数数


    var argTest=function(a,b,c,d){
        var numargs = arguments.length;     // 获取被传递参数的数值。
        var expargs = argTest.length;       // 获取期望参数的数值。
        console.log("实参数目为:"+numargs)
        console.log("形数数目为:"+expargs)
    
        console.log(arguments[0])         
        console.log(argTest[0])          //undefined 没有这种用法
    }
    //调用
    argTest(1,2,3)
    
    //结果
    实参数目为:3
    形数数目为:4
    1
    undefined 

    /*
     * 演示函数的caller属性.
     * 说明:(当前函数).caller:返回一个对函数的引用,该函数调用了当前函数
     */
    
    function callerDemo() {
        if (callerDemo.caller) {
            var a= callerDemo.caller.arguments[0];
            alert(a);
        } else {
            alert("this is a top function");
        }
    }
    function handleCaller() {
        callerDemo();
    }
    
    //callerDemo();
    //handleCaller("参数1","参数2");
    
    
    /*
     * 演示函数的callee属性.
     * 说明:arguments.callee:初始值就是正被执行的 Function 对象,用于匿名函数
     */
    function calleeDemo() {
        alert(arguments.callee);
    }
    //calleeDemo();
    //(function(arg0,arg1){alert("形数数目为:"+arguments.callee.length)})();
    
    
    /*
     * 演示apply,call函数的用法
     * 说明:作用都是将函数绑定到另外一个对象上去运行,两者仅在定义参数方式有所区别:
     *       apply(thisArg,argArray);
     *     call(thisArg[,arg1,arg2…] ]);
     *     即所有函数内部的this指针都会被赋值为thisArg
     */
    
     function ObjectA(){
        alert("执行ObjectA()");
        alert(arguments[0]);
        this.hit=function(msg){alert(msg)}
        this.info="我来自ObjectA"
     }
     
     function ObjectB(){
        alert("执行ObjectB()");
        //调用ObjectA()方法,同时ObjectA构造函数中的所有this就会被ObjectB中的this替代
        ObjectA.apply(this,arguments);//ObjectA.call(this);
        alert(this.info);
     }
     //ObjectB('参数0');
    
    
     var value="global 变量";
     function Obj(){
        this.value="对象!";
     }
     function Fun1(){
        alert(this.value);
     }
     //Fun1();
     //Fun1.apply(window); 
     //Fun1.apply(new Obj()); 

     

     

    argument.callee 是一个指向正在执行的函数的指针

  • 相关阅读:
    java enum类
    mvn filter autoconfig 产生自动配置
    Spring与Quartz的整合实现定时任务调度 以及crontab的用法
    网络广告术语CPC、CPM和CTR的含义和关系
    spring mvc3的注解@ResponseBody 自动返回jason
    Google Guava14.0 瓜娃学习笔记
    java中的各个数据结构区别
    org.apache.http.client.HttpClient; HttpClient 4.3超时设置
    maven test 运行 指定类或方法 打包 mvn clean assembly:assembly
    为什么要做url encode
  • 原文地址:https://www.cnblogs.com/lvchat/p/3830919.html
Copyright © 2011-2022 走看看