连云港led哪里做led屏 监控比较好

this为什么要赋值给局部变量that - Web前端当前位置:& &&&this为什么要赋值给局部变量thatthis为什么要赋值给局部变量that&&网友分享于:&&浏览:0次this为何要赋值给局部变量that
this的取值是跟它所在的上下文有关的。具体说就是跟它位于哪个函数中有关,以及跟这个函数被调用的方式有关。
在闭包里,还有在事件绑定的时候,由于作用的问题,this的上下文(作用域)变了,这个确实在JS中有点诡异,把this设置成一个局部变量后,外部函数里嵌套的函数就可以正确识别这个变量了。
tab.prototype.init=function(){
for(var i=0,len=this.span.i&i++){
that.span[i].index=i;
that.span[i].onclick=function(){
that.show(this.index);
this.show(1);
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 1234567891011 Copyright & &&版权所有问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
在一个js写的tab组件里看到这么一段代码,很奇怪为什么要把this赋给that?this的指向不一直是tab对象吗?
tab.prototype.init=function(){
for(var i=0,len=this.span.i&i++){
that.span[i].index=i;
that.span[i].onclick=function(){
that.show(this.index);
this.show(1);
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
把onclick这里当作回调函数来看,this.index的this指向的是that.span[i]。
that.span[i].onclick=function(){
that.show(this.index);
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
如果不在函数体内定义一个局部变量代替this,this的指向很容易会指向全局对象window或者其他对象,譬如
function myobj() {
var that =
this.name='jone';
this.count = function(clickbutton) {
$(clickbutton).live('click',function(){
alert(that.name);
var AD = new myobj();
AD.count('.cct');
这个例子中如果不用that代替this的话this会指向clickbutton,所以为了避免混淆,一般都会用一个that代替this;在这个例子中,作者可能是想避免this指向全局对象window,所以用that代替this,而且另外一个好处是用that储存了这个this对象,在下边的查询中就可以避免重复查询this对象,提高了运行的速度
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
这是一个闭包的应用,onclick函数将要调用当前tab实例对象的show方法如果如下调用而不保存当前对象的引用,那么在conclick函数的this将会是this.span[i]对应的对象,执行时可能因为这个对象没有show方法(不排除有)而报错~~~
this.span[i].onclick=function(){
this.show(this.index);
改成题目所示的写法,保存当前对象的引用,赋值给that,在click函数就能顺利的使用到实例对象
that.span[i].onclick=function(){
that.show(this.index);
代码还可以优化的哦
tab.prototype.init=function(){
var that=//赋值一次就好了
for(var i=0,len=this.span.i&i++){
this.span[i].index=i;
this.span[i].onclick=function(){
that.show(this.index);
this.show(1);
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
我就说一句:
this的取值是跟它所在的上下文有关的。具体说就是跟它位于哪个函数中有关,以及跟这个函数被调用的方式有关。
具体到你的例子,如果不用that替换一下,而是直接用this的话。第二个函数中的this就变成了that.span[i],而不是tab了。为什么呢?因为它所在的第二个函数是作为that.span[i]的方法被调用的。
同步到新浪微博
分享到微博?
你好!看起来你挺喜欢这个内容,但是你还没有注册帐号。 当你创建了帐号,我们能准确地追踪你关注的问题,在有新答案或内容的时候收到网页和邮件通知。还能直接向作者咨询更多细节。如果上面的内容有帮助,记得点赞 (????)? 表示感谢。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
扫扫下载 App3.2.变量声明与赋值,初始化,引用类型,this,作用域--Java语法_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
3.2.变量声明与赋值,初始化,引用类型,this,作用域--Java语法
|0|0|文档简介
深圳市远标培训中心|
总评分0.0|
&&3.2.变量声明与赋值,初始化,引用类型,this,作用域--Java语法
阅读已结束,如果下载本文需要使用2下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩8页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢属性节点-值与局部变量有什么区别?
比如我再前面板放置一个数值输入控件,在程序框图中分别右键创建-属性节点-值和创建-局部变量,这两者有什么区别?
建议用 局部变量 , 属性节点-值 的效率不高的。
记得主要是效率的区别
回复 1# yushouyuan 的帖子
& &&&用属性结点会触发界面线程,使得程序界面被强制更新。如果一个大型程序的某个子程序中用了属性结点,本来子vi的界面的前面板是不需要用户看见的,但由于使用了属性结点而在后台进行强制更新,这样很显然会影响程序执行的效率。你写一个简单的程序验证一下就看出来了。
& & 局部变量也不是越多越好,它使labview无法重用缓存,不得不开辟新的缓存区,可以尽量用移位寄存器等结构来强制内存重用。
自己在测量与测试中搜的英文版的
属性节点的Value和局部变量区别
There are some subtle differences that are detailed here.
The property node allows the reading and writing of a single control within a cluster whereas a local variable and
terminal will always refer to the entire cluster.&&A property node can update the value from a remote location, not
on the diagram associated with the panel.&&If you are using a non-strict control reference, then the datatype of the value may be a variant rather than the actual datatype of the control.
Due to these differences, there are also some performance differences. If updating something like a graph, you
would sometimes need to position two cursors, change some colors, and write the data to the graph.&&In the past
this would require two nodes, one for properties and another (terminal or local) for the value.&&This resulted in two
graph redraws.&&Adding the value property to the existing node and not using the terminal or local will result in one
less graph redraw, which may or may not be important for your application.
On the other hand, because they can update a subset of the data, property nodes do not implement the same
shortcuts that the locals and terminals do.&&When updated with the same value over and over, a local and terminal
will not redraw the control, but the value property will.&&Additionally, for controls that are updated very quickly i.e.
more than 50 times a second, terminals and locals have the option to amortize the redisplays and save CPU cycles
that would otherwise be spent redrawing things you wouldn't see anyway.&&There is the option to synchronize the
display with the diagram updates, and the value property is basically stuck using this synchronized version each
time.&&This is identical to how controls and indicators updated before LabVIEW 5, and it is still how the displays
are done on single threaded systems such as any Macintosh with OS 9.x or previous.
To see this, drop a for loop, wire i to an indicator and have the loop execute 1,000,000 times.&&Initially, update the
indicator with the terminal. Run the program and it will take a fraction of a second.&&The indicator will display 0, a
few numbers in between, then 999,999.&&Move the terminal out of the loop and update with a local.&&You will see
the same.&&Now popup on the indicator and choose synchronous display -- it is in the Advanced option in LabVIEW 6.x and 7.x.&&Now you will probably need to abort the VI because it will
take several minutes to run because it is drawing every single number to the screen making a nice blur of digits for
you to look at.&&Now, delete the local and use a property node instead.&&Regardless of the setting of the synchronous
display, you will see the display is showing each and every one of the updates.
The fact that the value can be set from subVIs is a great feature, but it will make your diagrams more difficult to
debug if you make a mistake.&&When a string or the indicator has the wrong value being displayed, you need to
find the code that did the update.&&If only using the terminal, there is only one place the indicator is updated, and
you can work backwards from the terminal.&&If using locals, then you have a list of places to work backwards from.
If you are passing the control reference to the indicator to some number of subVIs, then you can have many
potential &updaters&, including dynamic ones.&&So, while this is a nice feature for modularizing code and separating
it from the display, be careful or it will have debugging expenses that you may not want.
The following link details LabVIEW performance and memory management. It includes descriptions of tools for
profiling your VIs to see changes in memory and running time
回复 5# yushouyuan 的帖子
不错,赫赫
原来在这里
我就记得看过的
GSDzone.net. All rights reserved.this的取值是跟它所在的上下文有关的。具体说就是跟它位于哪个函数中有关,以及跟这个函数被调用的方式有关。
在闭包里,还有在事件绑定的时候,由于作用的问题,this的上下文(作用域)变了,这个确实在JS中有点诡异,把this设置成一个局部变量后,外部函数里嵌套的函数就可以正确识别这个变量了。
tab.prototype.init=function(){
for(var i=0,len=this.span.i&i++){
that.span[i].index=i;
that.span[i].onclick=function(){
that.show(this.index);
this.show(1);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

我要回帖

更多关于 监控led显示屏 的文章

 

随机推荐