js 中var that=this

js中经常出现var that=this,为什么这么做?

http://stackoverflow.com/questions/4886632/what-does-var-that-this-mean-in-javascript 

问题,回答:

一个回答:
$(‘#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 this frequently changes when you change the scope by calling a new function, you can‘t access the original value by using it. Aliasing it to that allows you still to access the original value of this.

另一个回答:From Crockford

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.

js 中var that=this,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。