草庐IT

this_key

全部标签

javascript - 停止在临时变量中保存 'this'

我一直不得不将this保存在一个临时变量中,以便在其他函数中访问它。例如,在下面的两个方法中,我将this保存在that变量中:startTimer:function(){varthat=this;if($('#defaultCountdown:hidden'))$('#defaultCountdown').show('slow');shortly=newDate();shortly.setSeconds(shortly.getSeconds()+5);$('#defaultCountdown').countdown('change',{until:shortly,layout:'Ne

javascript - jquery 在自定义函数中使用 (this)

我创建了一个小的jquery脚本,但在自定义函数中使用(this)时遇到问题。这是代码:jQuery("li").click(function(){varscrollTop=jQuery(window).scrollTop();if(scrollTop>0){jQuery('html,body').animate({scrollTop:0},'slow',function(){fadeItems();});}else{fadeItems();}});functionfadeItems(){varslogan=jQuery(this).children('p').html();jQuer

javascript - 'this"在闭包中是如何工作的?

我到了thisdocument这表示这里发生了关闭:functionaddHandler(){document.getElementById('el').onclick=function(){this.style.backgroundColor='red';};}虽然这段代码移除了闭包:functionaddHandler(){varclickHandler=function(){this.style.backgroundColor='red';};(function(){varel=document.getElementById('el');el.onclick=clickHandl

javascript - nodejs - 这是哪个 "this"?

所以这是一个尴尬的问题,但我正在学习NodeJS,我有一个问题。在Java中,当我从对象调用方法时,this实例保持不变(如本例所示)。privateTestinst;publicTest(){inst=this;this.myFunction();}privatevoidmyFunction(){System.out.println(inst==this);}这会返回true(理论上,这是我头脑中的代码)。但是,在NodeJS中,当我尝试做类似的事情时失败了。varMyObject=function(){this.other=newOtherObject();this.other.o

javascript - Function.prototype.call 在严格模式之外改变 this 的类型;为什么?

varexample=function(){console.log(typeofthis);returnthis;};在严格模式下:example.call('test')#prints'string'否则,example.call('test')#prints'object'然而,console.log(example.call('test'))版画test(如你所料)为什么Function.call更改typeof'test'==='string'绑定(bind)到this里面example? 最佳答案 当使用call()并将t

javascript - Firebug 控制台窗口范围。为什么 "this"不总是相同的?

Firebug控制台作用域。为什么“这个”不总是一样的?不应该一直是“window”吗? 最佳答案 控制台中this的值将与当前正在执行的代码中this的值相同。考虑:-functionouter(){//thisiswindowvarx={n:12};varfn=function(){//thisisobject{n:12}alert(this.n);}fn.call(x);}...如果你在x={n:12}行打断点,切换到控制台你会发现this是窗口。但是,当您进入alert行时,控制台中的this是x变量持有的对象。IOW在执行

javascript - 我可以将 "this"作为参数传递给 javascript 中的另一个函数吗

我有这个:$('#sliderli').click(function(){varstepClicked=$(this).index();alert(stepClicked);if(stepClicked!=0){$('#cs_previous').removeClass('cs_hideMe');}else{$('#cs_previous').addClass('cs_hideMe');}$('li.cs_current').removeClass('cs_current');$($(this)).addClass('cs_current');moveToNextImage(stepC

javascript - JSON 响应对象 : "pretty" keys and larger response or short keys and smaller response?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭8年前。Improvethisquestion我的实时网络应用发出ajax请求以获取JSON编码数据响应。返回的数据通常以对象数组的形式存在。由于数组通常有很多元素(尽管发送的数据由服务器gzip压缩),为了将响应大小保持在最小值,我保留了键在响应中非常简短。例如,我不使用description:,而是使用d:,而不是使用width:,我使用w:等等...这样做会减少响应的大小,但在客户端,非常短的非人类可读键会降低JavaScrip

javascript - 在 javascript 中的 map() 中动态设置 key ?

所以我知道如何像这样动态设置key:varhashObj={};hashObj[someValue]=otherValue;但是我还没有看到关于map()的任何答案:varlist=['a','b','c'];varhashObject=list.map(function(someValue){return{someValue:'blah'};});//shouldreturn:[{'a':'blah'},{'b':'blah'},{'c':'blah'}];我知道我可以在for循环等中执行此操作,但这在javascript中仅使用map()是不可能的吗?

javascript - 诗乃JS : Is there a way to stub a method on object argument's key value in sinon js

我想在以下响应中模拟对obj.key3值的不同响应。就像ifobj.key3=true然后返回与obj.key3=false不同的响应functionmethod(obj){returnanotherMethod({key1:'val1',key2:obj.key3});} 最佳答案 您可以使用.withArgs()和对象匹配器根据调用它的参数使stub返回(或执行)某些操作。例如:varsinon=require('sinon');//Thisisjustanexample,youcanobviouslystubexistingm