首页 > Web开发 > 详细

js 中var that=this

时间:2014-07-18 16:02:36      阅读:1389      评论:0      收藏:0      [点我收藏+]

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,布布扣,bubuko.com

js 中var that=this

原文:http://www.cnblogs.com/youxin/p/3852163.html

(1)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!