为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁星淼淼 2018-09-13 14:09:20
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属性,不能传入参数
查看完整描述

1 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

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();                                                                  
                                                                                    
}())


查看完整回答
反对 回复 2018-10-18
  • 1 回答
  • 0 关注
  • 534 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信