这不是一个非常重要的问题,但我们开始吧..如何避免在jQuery事件处理程序中使用var_this=this?即我不喜欢这样做:var_this=this;$(el).click(function(event){//use_thistoaccesstheobjectand$(this)toaccessdomelement});下面2种方式都不理想$(el).click($.proxy(function(event){//lostaccesstothecorrectdomelement,i.e.event.targetisnotgoodenough(seehttp://jsfiddle.
所以我知道在使用$.fn.each、$.fn.bind等时,它是this的标准jQuery中的关键字将回调链接为DOM元素。我知道至少在我的开发中我通常希望DOM元素包装在一个jQuery集合中——90%的时间我最终都在做var$this=$(this)。我确信他们选择绑定(bind)到未包装的元素有一个很好的(可能是基于性能的)理由,但有人知道它到底是什么吗?这是我觉得知道答案的事情之一,可能会为更深层次地理解图书馆和语言打开大门。 最佳答案 Iamsuretherewasagood(likelyperformance-based
我试图理解使用关键字“this”或者它在jQuery中代表什么与Backbone等MVC框架的区别。下面是每个的2个代码示例;所以在jQuery中,我们有$("#result").click(function(){$(this).html(someval);})在Backbone中,我们的代码为;varHandlebarsView=Backbone.View.extend({el:'#result'initialize:function(){this.template=Handlebars.compile($('#template').html());},render:function
看了很多关于单例模式的文章,并做了一些测试,我发现单例模式和这样的单例模式没有区别(http://jsfiddle.net/bhsQC/1/):varTheObject=function(){varinstance;functioninit(){varthat=this;varfoo=1;functionconsoleIt(){console.log(that,foo);}return{bar:function(){consoleIt()}};}return{getInstance:function(){if(!instance){instance=init();}returninst
在SecretsofJavascriptClosures,StuartLangridge提供了一段代码来演示闭包在.onclick回调中的常见用法,并解释如下:link.onclick=function(e){varnewa=document.createElement("a");varthat=this;document.body.appendChild(newa);newa.onclick=function(e){that.firstChild.nodeValue="reset";this.parentNode.removeChild(this);}}我最近偶然发现了KyleSim
我有:classAdminHomeController{privateconfig1;//Itrieddifferentvariationsherebutnoneworkedpublicconfig2;//constructor(private$scope:IAdminHomeControllerScope){this.config=$scope.config;//{this.config.clear();};}此代码有效,this.config具有我需要的所有方法。但是有没有办法我可以删除对this的需要吗?我希望能够编写以下代码:configChanged=(clear)=>{co
我使用React和jQuery。这是我的代码的一部分。在挂载React组件之前,我执行ajax请求以了解用户是否已登录。应该在响应返回状态码200时设置状态。我是否错误地使用了bind(this)?componentWillMount:function(){$.ajax({url:"/is_signed_in",method:"GET",dataType:"json"}).success(function(response){this.setState({signedIn:response.signed_in,currentUser:$.parseJSON(response.curre
根据JesseMonroy650的要求编辑:我之前做过几个混合应用程序,但都没有商业发布。我正在使用PhoneGapBuild。位于此处:https://build.phonegap.com/我没有使用任何桌面应用程序。原创内容:我正在尝试从PhoneGapBuild中的Facebook获取数据。我有一个简单的脚本,看起来它应该基于API工作:document.addEventListener("deviceready",onDeviceReady,false);functiononDeviceReady(){varfbLoginSuccess=function(userData){a
我在我的React应用程序中的经过身份验证的组件上大量使用localStorage,以从本地存储获取用户详细信息以及在登录时存储它们。当我构建我的应用程序时,它抛出ReferenceError:localStorageisnotdefined。我知道这可能是因为Node无法访问localStorage,因此出现错误。我该如何解决这个问题?这是我在组件中使用的代码示例。importReact,{PropTypes}from'react';importAccountBasicInfofrom'../components/AccountBasicInfo';exportclassAccoun
我试图在我的箭头函数中访问它:importmyObjectfrom'../myObjectPath';exportconstmyClass=Fluxxor.createStore({initialize(){this.list=[];this.id=null;},myOutsideFunction(variable1){//herethisinNOTundefinedmyObject.getMyList(this.id,(myList)=>{//herethisinundefinedthis.list=myList;}});)};但是在回调函数中的箭头函数中,this是未定义的!!我正