草庐IT

log_this

全部标签

javascript - "this"上下文输出无法理解

我很难理解下面的代码。functionfoo(){console.log(this.a);}varobj={a:2,foo:foo};vara=4;obj.foo();setTimeout(obj.foo,100);setTimeout(obj.foo.bind(obj),100);它的输出为2、4、2,我无法理解。 最佳答案 第一种情况,obj.foo();foo中的this将指向obj,因为您已将该函数分配为该特定对象的属性。第二种情况,setTimeout(obj.foo,100);在setTimeout中,传递的函数将在窗口

javascript - 为什么 alert();在 console.log() 之前运行;

我的问题与其他问题有何不同我使用的是ES6语法。我查看的其他问题使用ES5语法。问题为什么alert();在console.log();之前运行?我是否可以让console.log();在alert();之前执行?我的代码console.log("Hello!");alert("Hi!"); 最佳答案 console.log("Hello!");setTimeout(()=>alert("Hi!"),0);基本上:从技术上讲,console.log()首先被调用。†然而,浏览器实际上重新绘制自身或控制台更新也需要一些时间。不过,在它

javascript - 防止 this.state 与 setState 一起使用

Thereference状态:setState()doesnotalwaysimmediatelyupdatethecomponent.Itmaybatchordefertheupdateuntillater.Thismakesreadingthis.staterightaftercallingsetState()apotentialpitfall.Instead,usecomponentDidUpdateorasetStatecallback(setState(updater,callback)),eitherofwhichareguaranteedtofireaftertheupd

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 - Google Chrome console.log 乱序?

这个问题在这里已经有了答案:IsChrome’sJavaScriptconsolelazyaboutevaluatingobjects?(7个答案)关闭5年前。有人可以解释以下两个输出吗?代码1:console.log(itemsAry);//loadNextItem();functionloadNextItem(){varitem=itemsAry.shift();console.log(item);}结果:["cat-53","cat-57","cat-51","cat-10","cat-55","cat-56","cat-5","cat-50","cat-3","cat-54",

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 - 有没有办法使用 Javascript 读取 console.log?

标题是不言自明的..有没有一种方法可以使用Javascript读取输出到console.log的任何内容,直到您决定读取它为止? 最佳答案 你可以围绕它做一个代理,比如:(function(win){varncon=win.console;varcon=win.console={backlog:[]};for(varkinncon){if(typeofncon[k]==='function'){con[k]=(function(fn){returnfunction(){con.backlog.push([newDate(),fn,a

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