javascript 函數取得調用對象的信息。?
function testObject(){ this.commonFun=function() { console.log(arguments.callee.caller); } }(function () {function test1() { this.test = "test1"; this.testFun = f<愛尬聊_百科>unction () { (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天前
可以改寫成:
miynefst 1天前
commonFun的caller就一直是testObject,你這種調用方法,做不到通過caller來顯示更上一層的調用關系。
xiaominmin123 1天前
你的這個需求,用繼承來實現是最合乎邏輯的。如果是需要子類去實現,可以用接口的方式。
青爭汶魚 1天前
首先,你的設計上就有問題,你callee,caller這些取到的都是函數,不是實例,看下面的例子:
