我想知道是否有可能以某種方式阻止this
關鍵字轉化爲_this
裏面的胖箭頭回調(=>
)?Coffeescript - 'this'總是被'_this'替換爲胖箭頭回調中的'_this'
例如:
class someClass
someMethod: ->
$(document).on 'click', '.myclass', (e) =>
# doing things with right context this, it's ok
@anotherMethod()
@oneMoreMethod()
# but here I need jQuery ``this`` pointing to element
$el = $ this # this is transformed into ``_this`` :(
也許我錯過了一些選項,或運營商?
UPDATE 我知道像self = this
的伎倆,但我認爲CS有什麼更優雅..
嘗試在我的具體情況下使用'$(e.target)' – elclanrs
「e.target''不是解決方案,我需要使用''this''來獲取具體屬性,我選擇的屬性而不是它的孩子。 – Kosmetika
一個奇怪的伎倆得到這個工作是用'這個'替換'this',它使用正常的'this'按預期。第一個反引號和這個之間需要空格。看到它[這裏](http://coffeescript.org/#try:class%20someClass%0A%0A%20%20someMethod%3A%20-%3E%0A%20%20%20%20%24(document) %。對20'click '%2C%20'.myclass' %2C%圖20(e)%20%3D%3E%0A%20%20%20%20%20%20%23%20doing%20things%20with %20right%20context%20this%2C%20IT的%20ok%0A%20%20%20%20%20%20%40anotherMethod()%0A%20%20%20%20%20%20%40oneMoreMethod()%0A %20%20%20%20%20%20%24%20%3D%20%24%20%60%20this%60) –
vcsjones