码迷,mamicode.com
首页 > Web开发 > 详细

js 中var that=this

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

标签:style   http   java   color   strong   io   

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

标签:style   http   java   color   strong   io   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!