2011-06-13 61 views
8

如果我有這樣的功能:如何獲得調用者函數的'this'值?

function foo(_this) { 
    console.log(_this); 
} 

function bar() {} 
bar.prototype.func = function() { 
    foo(this); 
} 

var test = new bar(); 
test.func(); 

那麼bartest實例得到記錄。

但是,爲了這個工作,我必須通過bar.prototype.func函數中的this。我想知道是否有可能獲得相同的this價值沒有通過this

我試過使用arguments.callee.caller,但是這返回原型函數本身,而不是原型函數內的this值。

是否可以通過在原型函數中僅調用foo()來記錄testbar的實例?

+1

不,你不能訪問的函數調用堆棧的上下文。你必須傳遞這個背景。 – Raynos 2011-06-13 14:54:12

+0

如果你已經打算創建一個特殊用途的函數來記錄連接到酒吧的原型,是否有一個原因你不想傳遞這個'this'?除了學習目的使用這種模式之外,還有其他原因嗎? – TNi 2011-06-13 15:01:49

+0

@TNi:這不一定有實用的目的,但在我的情況下會很有用。我試圖通過傳遞一個函數並綁定到一個函數來執行循環的多級循環,該函數執行的功能與for循環一樣。在一個普通的for循環中,可以訪問'this',但這樣看來我需要將'this'傳遞給我的函數。 – pimvdb 2011-06-13 15:04:52

回答

3

如果問題是「沒有通過這個(通過任何手段)」,然後回答是沒有

值可以是儘管通過了替代方法。例如使用全局變量(在Bar類中)或會話或cookie。

function bar() { 

     var myThis; 

     function foo() { 
      console.log(myThis); 
     } 

     bar.prototype.func = function() { 

      myThis = this; 
      foo(); 
     } 
    } 

    var test = new bar(); 
    test.func(); 
+2

你仍然傳遞'this'。我發現'foo.apply(this)'是一個更清潔/不那麼討厭的人。 – Raynos 2011-06-13 15:03:17

+1

我沒有將它傳遞給它,而是將值設置爲可在Bar類的名稱空間內訪問的變量。這是一個很好的方法。 – 2011-06-13 15:05:19

+1

@Raynos:這是正確的,但我接受是因爲聲明這是不可能的。這基本上回答了我的問題。 – pimvdb 2011-06-13 15:05:50

1

我認爲bar範圍內調用foo應該工作:

function foo() { 
    console.log(this.testVal); 
} 

function bar() { this.testVal = 'From bar with love'; } 
bar.prototype.func = function() { 
    foo.call(this); 
} 

var test = new bar(); 
test.func(); //=> 'From bar with love' 
+0

謝謝,但我仍然需要傳遞'this'的值。我的意思是我應該可以通過在'func'裏面調用'foo()'來獲得'foo'內的'this'。 – pimvdb 2011-06-13 14:53:37

+0

但是,這是在這裏發生的事,還是我誤解了你的問題?在我的回答中調用foo使得來自實例test的'this'可用於foo,如通過'log'記錄它的testVal屬性所證明的。 – KooiInc 2011-06-13 14:57:47

+0

我很抱歉不清楚。我的意思是在'foo' *內獲得'func'的'this'',而不真正將'this'傳遞給'func'。 – pimvdb 2011-06-13 14:59:20

0

您可以在不更改外部功能的情況下執行此操作,但必須更改調用方式。

您無法獲取調用者的上下文,但可以使用方法applycall調用的函數上設置this屬性。有關this的解釋,請參閱this reference

function foo() 
{ 
    console.log(this); 
} 

function bar() 
{ 
    bar.prototype.func = function func() 
    { 
     foo.apply(this); 
    }; 
} 

var test = new bar(); 
test.func(); 

通常如果使用this,它在面向對象的上下文中。試圖用另一種方法調用對象的方法可能表明設計不佳。多解釋一下你正在嘗試實現更多適用的設計模式。

有關javascript OOP範例的示例,請檢查my answer here

1

這是怎麼回事?

"use strict"; 
var o = { 
    foo : function() { 
     console.log(this); 
    } 
} 

function bar() {} 
bar.prototype = o; 
bar.prototype.constructor = bar; 
bar.prototype.func = function() { 
    this.foo(); 
} 

var test = new bar(); 
test.func(); 

或者這樣:

"use strict"; 
Function.prototype.extender = function(o){ 
    if(typeof o == 'object'){ 
     this.prototype = o; 
    }else if (typeof o == 'function') { 
     this.prototype = Object.create(o.prototype); 
    }else{ 
     throw Error('Error while extending '+this.name); 
    } 
    this.prototype.constructor = this; 
} 
var o = { 
    foo : function() { 
     console.log(this); 
    } 
} 

function bar() {} 
bar.extender(o); 
bar.prototype.func = function() { 
    this.foo(); 
} 

var test = new bar(); 
test.func(); 
相關問題