草庐IT

Java——this关键字

全部标签

javascript - 为什么要创建一个值为 this 的变量

我在JavaScript中看到过很多这种情况,我确实记得找出原因,但我不记得答案了。我猜这与范围和在“类”外部调用的函数有关,但为什么要这样做(最好概述一个示例):functionmyClass(){varself=this;//...this.myArray=[];this.myFunc=function(){alert(self.myArray.length);};} 最佳答案 为了锁定变量作为closure的一部分.例如:MyClass.prototype.doStuff=function(){this.foundItems=

javascript - 谷歌地图劫持​​了 iphone 的滚动(触摸事件)——如何恢复?

我使用JQuery。我使用googlemapsapiv3。现在,在我的iPhone上,如果我将手指放在map区域,我无法向下滚动页面。map选项中的draggable:false不起作用。它只是阻止map在内部移动。找到了这些类似的问题,但无法从中得到答案:HowcanIdisablescrollingontheGoogleMapsmobilelayout?EmbedGoogleMapsonpagewithoutoverridingiPhonescrollbehaviorGoogleMapsAPI;SuppressMapPanningtoenablePageScrolling有什么简单

javascript - Google+ 使用 JavaScript 登录——立即调用两次回调

我正在尝试按照Google+指南使用我自己的按钮启动Google+登录流程。关于回调函数,gapi.auth.signIn引用说(引用):"Afunctionintheglobalnamespace,whichiscalledwhenthesign-inbuttonisrenderedandalsocalledafterasign-inflowcompletes."出现Google登录对话框,要求我登录,但在与该对话框进行任何交互之前,回调被立即调用了两次。两次我都得到类似的authResult,error="immediate_failed",error_subtype="acces

javascript - 为什么 `this` 在 ES6 箭头函数中不起作用?

这个问题在这里已经有了答案:ArrowFunctionsandThis[duplicate](5个答案)关闭7年前。这是我的代码:'usestrict';letobj={username:'HansGruber',hello:()=>'hello,'+this.username};console.log(obj.hello());但输出是:hello,undefined。我希望输出为:你好,HansGruber。我想我还没有理解箭头函数中的this。谁能给我一个明确的解释?

javascript - 在匿名内部使用 'this',IDE : potentially invalid usage

就最佳实践而言,下一个功能(实际有效)是否不好?IDE警告我'Potentiallyinvalidusageof'this'.ChecksforJavascript'this'tobeinthesameclosureoroutercontent.$(document).on('change','#select-all',function(){if(this.checked){$(this).closest('table').find('input[name="row-id"]').each(function(){this.checked=true;//Here})}else{$(thi

javascript - jQuery this.remove() -vs.- $ ('#id' .remove() 在 Internet Explorer (IE 9+)

为什么this.remove()在IE9+中不起作用?$('#nextButton1').on('click',function(){this.remove();//worksinallbrowsersbutIE9+});$('#nextButton2').on('click',function(){$('#nextButton2').remove();//worksinallbrowsers});JSFiddleliveversion 最佳答案 那是因为您正在使用并非所有浏览器都支持的ChildNode.remove()方法。th

javascript - 为什么将字符串作为 "this"传递会导致这种奇怪现象?

我试图理解为什么javascript会做一些(对我来说)意想不到的事情。这里有一些代码,纯粹是为了举例。换句话说,我实际上并不想扩展String(我实际上绑定(bind)到函数和东西)。所以这是没有库的纯JavaScript。vars='blah';String.prototype.foo=function(){console.log('this===s:',this===s);console.log('this==s:',this==s);console.log('typeofthis:',typeofthis);console.log('typeofs:',typeofs);con

javascript - 主干错误 : Uncaught TypeError: Object function (){ parent. apply(this, arguments); } 没有方法 'on'

知道为什么我在调用collection.fetch时会收到此错误吗?在这段代码中抛出:这是触发错误的代码:$(document).ready->SearchResult=Backbone.Model.extendSearchResults=Backbone.Collection.extendurl:"/backbone/search"model:SearchResultparse:(response)->console.logresponsenewSearchResultid:response.idtitle:response.titlesearchResults=newSearchR

javascript - 为什么此代码有效 : "(1,eval)(' this')"

为什么下一个代码是有效的Javascript代码?varglobal=(1,eval)('this');alert(global); 最佳答案 那是因为commaoperator返回它的第二个操作数(并计算两者)。您问题中的代码相当于:1;varglobal=eval('this');alert(global); 关于javascript-为什么此代码有效:"(1,eval)('this')",我们在StackOverflow上找到一个类似的问题: https

javascript - react : How to access refs in this. props.children

我想调用一个子组件的函数。是否有可能在React中从this.props.children获取引用。varComponentSection=React.createClass({componentDidMount:function(){//Howtoaccessrefsinthis.props.children?this.refs.inner.refs.specificPanel.resize();},render:function(){return({this.props.children});}});varPanel=React.createClass({resize:functi