我在一個新的代碼庫中遇到過很多次,我在看,並且想知道是否有任何正確的推理呢?聲明var的原因是什麼= this;在JavaScript?
4
A
回答
2
您可以使用var that = this;
是爲了保持對當前this
對象的引用,當後面this
會指向別的東西。
實施例(taken from here):
$('#element').click(function(){
// this is a reference to the element clicked on
var that = this;
$('.elements').each(function(){
// this is a reference to the current element in the loop
// that is still a reference to the element clicked on
});
});
+1
與我的答案相同,但有一個例子,發揮得很好:) – ChrisCM
1
有時JavaScript中的this
的含義根據範圍而改變。 this
裏面的構造函數意味着不同於this
裏面的函數。這是關於它的good article。
0
如果您想訪問特定函數調用範圍之外/之內的「this」,其中「this」可能已更改。我只能想到一個例子。
相關問題
- 1. 什麼原因var $ this =這個
- 2. 聲明指向* this *對象的優點或原因是什麼?
- 3. Javascript變量聲明:什麼是「var myVariable = {}」?
- 4. Var聲明在Javascript
- 5. 在Heroku中,「Procfile聲明類型 - >(無)」的原因是什麼?
- 6. 爲什麼VAR聲明快不是讓
- 7. 警告的原因是什麼:函數'swprintf_s'的隱式聲明?
- 8. 什麼VAR = VAR &&「*」的意思是在Javascript
- 9. 在XML文件中指定var類型的原因是什麼?
- 10. $ var :: staticfunction()確定但是$ this-> var :: staticfunction()NOT。基本原理是什麼?
- 11. JavaScript中斷點的原因是什麼?
- 12. .bind(this)vs var context = this在Javascript中 - 什麼是最好的風格?
- 13. 是什麼原因
- 14. 在JavaScript中,「聲明職位」是什麼?
- 15. 在javascript中爲什麼我們使用var來聲明變量?
- 16. PHP,$ this - > {$ var} - 這是什麼意思?
- 17. 爲什麼我必須聲明var = Class.new?
- 18. 在hibernate Property標籤中聲明非空字段的原因是什麼?
- 19. 爲什麼我不能刪除用var聲明的javascript值?
- 20. RejctedExecutionException的原因是什麼?
- 21. 'System.IO.IsolatedStorage.IsolatedStorageException'的原因是什麼?
- 22. BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE的原因是什麼?
- 23. 什麼原因導致「聲明中沒有表」消息?
- 24. ChangeConflictException的原因是什麼?
- 25. 什麼是UnsatisfiedLinkError的原因?
- 26. UnsupportedClassVersionError的原因是什麼?
- 27. 什麼是聲明
- 28. 什麼是聲明
- 29. 顯式聲明默認的Java類序列化方法的原因是什麼?
- 30. var聲明期望聲明
相關:http://stackoverflow.com/questions/7234282/what-is-the-reason-for-var-this-this?rq=1 – Niko
取代碼,並嘗試用'this'替換'that'在哪裏使用,你會發現它可能不起作用。 – 2013-05-28 14:26:27
http://stackoverflow.com/a/4886696/1651233 – BobTheBuilder