javascript 函数取得调用对象的信息。

function testObject(){            this.commonFun=function() {                console.log(arguments.callee.caller);
            }
        }

        (function () {            function test1() {                this.test = "test1";                
        this.testFun = function () {
                    (new testObject()).commonFun();
                }
            }            var test1 = new test1();
            test1.testFun();

        }());


        (function () {            function test2() {                this.test = "test2";                
        this.testFun = function () {
                    (new testObject()).commonFun();
                }
            }            var test2 = new test2();
            test2.testFun();
        }());

上面的console.log() 打印的调用函数,但我现在想要的是调用对象的信息,如,test1对象调用的返回的是test1的test属性,就是输出 test1,
test2对象调用的返回的是test2的test属性,就是输出 test2

函数commonFun作用是输出调用对象的的某些属性,就像上面的test属性,不能传入参数


繁星淼淼
浏览 535回答 1
1回答

Qyouu

function testObject(){                                                                 console.log('is testObject');                                                        this.commonFun=function() {                                                            console.log(arguments.callee.caller);                                              }                                                                                  }commonFun的caller就一直是testObject,你这种调用方法,做不到通过caller来显示更上一层的调用关系。可以改写成:function testObject(){                                                                 console.log('is testObject');                                                        this.commonFun=function(arg1) {                                                            console.log(arguments.callee.caller);    console.log(arg1);   }                                                                                  } (function () {                                                                                                                                                              function test1() {                                                                     this.test = "test1";                                                                                                                                         this.testFun = function () {                                                           (new testObject()).commonFun(this.test);                                                    }                                                                                  }                                                                                                                                                                         var test1 = new test1();                                                             test1.testFun();                                                                                                                                                        }())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript