What does var that = this; mean in javascript?
2015-01-16 10:41
417 查看
[b]Question:[/b]
In a javascript file I saw:
What does this do precisely?
Answer1:
I'm going to begin this answer with an illustration:
My answer originally demonstrated this with jQuery, which is only very slightly different:
Because
Personally, I dislike the use of
Answer2:
By convention, we make a private that variable. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions.
……………………………………
【转载】http://stackoverflow.com/questions/4886632/what-does-var-that-this-mean-in-javascript
In a javascript file I saw:
function Somfunction(){ var that = this; ... }
What does this do precisely?
Answer1:
I'm going to begin this answer with an illustration:
var colours = ['red', 'green', 'blue']; document.getElementById('element').addEventListener('click', function() { // this is a reference to the element clicked on var that = this; colours.forEach(function() { // this is undefined // that is a reference to the element clicked on }); });
My answer originally demonstrated this with jQuery, which is only very slightly different:
$('#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 }); });
Because
thisfrequently changes when you change the scope by calling a new function, you can't access the original value by using it. Aliasing it to
thatallows you still to access the original value of
this.
Personally, I dislike the use of
thatas the alias. It is rarely obvious what it is referring to, especially if the functions are longer than a couple of lines. I always use a more descriptive alias. In my examples above, I'd probably use
clickedEl.
Answer2:
By convention, we make a private that variable. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions.
……………………………………
【转载】http://stackoverflow.com/questions/4886632/what-does-var-that-this-mean-in-javascript
相关文章推荐
- In Javascript, what does this underscore mean?
- __memcpy_sse2_unaligned - what does this mean in detail?
- What does '>/dev/null 2>&1' mean in this article of crontab basics
- What does (Aborted) mean in HttpWatch?
- So, what does the asterisk(*) mean in AssemblyVersion("1.0.*")?
- What does "Rxlch" mean in ENCODE?
- javascript中var that=this
- What does idl attribute mean in the W3C html5 standard document?
- What does the distance attribute in DMatches mean?
- What does appending “?v=1” to CSS and Javascript URLs in link and script tags do?
- What Does “Neurons that Fire Together Wire Together” Mean?
- What does it mean by Single-Leg & Multiple Leg in Stock Trading?
- The usage of the Javascript method call and apply(what is the new and object.create difference.)(What does call(null) mean?)
- (转载)What does (Aborted) mean in HttpWatch?
- What does that mean: Work smart, not just hard?
- what does mean "Units" in apple app sales report?
- what does the three dots in “doInBackground(URL… urls)” mean?
- What does => and () => mean in Scala
- What does it mean to write `static void * ptr = &ptr` in objective-c?
- What does [SimpleType] and [Compact] in bindings mean?