草庐IT

send_this_email

全部标签

javascript - 警告 - 全局 this 对象的危险使用

在GoogleClosureCompiler中我收到警告WARNING-dangeroususeoftheglobalthisobject这是一个例子。错误行和偏移量指的是单词this的开头functionaToggle(){if(shown)toggle.show()elsetoggle.hide()$(this).text(shown?'Clicktohide':'Clicktoshow')shown=!shown}link.onclick=aToggle我只想将其更改为匿名方法,但我在文件的其他地方重新使用了aToggle,因此需要对其进行命名。我可以将aToggle标记为/**

javascript - 我们可以将 $(this) 与其他选择器一起使用吗?

例如:$("#"+$(this).attr("id")+"option[value='0']")我们能否将代码简化为类似的东西$(this+"option[value='0']") 最佳答案 这些将与您的第一个语句执行相同的操作:找到具有0作为它们的value的option元素并且是(不限制直接)this的child。$(this).find('option[value="0"]')或$('option[value="0"]',this)资源:jQuery.find()UnderstandingcontextinjQueryjsFi

javascript - Javascript 对象中的 var 与 this

我正在为node.js开发一个网络框架。这是代码;functionRouter(request,response){this.routes={};varparse=require('url').parse;varpath=parse(request.url).pathname,reqRoutes=this.routes[request.method],reqRoutesLen=reqRoutes.length;.....//morecode};我应该把所有的var都改成这个吗,像这样:functionRouter(request,response){this.routes={};thi

javascript - 匿名函数中的 "this"?

在JohnResig的书“ProJavascripttechniques”中,他描述了一种使用以下代码生成动态对象方法的方法://CreateanewuserobjectthatacceptsanobjectofpropertiesfunctionUser(properties){//Iteratethroughthepropertiesoftheobject,andmakesure//thatit'sproperlyscoped(asdiscussedpreviously)for(variinproperties){(function(){//Createanewgetterfort

JavaScript 原型(prototype) 'this' 问题

这是我上一个问题的后续问题。Simplejavascriptprototypeissue我对使用JavaScriptprototype有点陌生,对于第二篇文章感到抱歉。我想将被点击的元素id分配给this.name数组。task.prototype.init=function(){this.name=[];//this.namearrayhastobedefinedherefor(vari;ielement.this.name.push(this.id);returnfalse;}任务的任何提示? 最佳答案 您的原型(prototy

javascript - 在 Backbone 中 this.model 是未定义的,为什么?

我到处寻找答案,但对我的发现并不满意。问题是,我正在学习AddyOsmani的教程以在Backbone中制作“Todo”应用程序,但是当我查看控制台时,我收到一条错误消息,提示this.model未定义.我什至试过这个SO答案Backbonemodelerrordisplayedinconsole,但我仍然遇到同样的错误。请告诉我哪里出了问题。顺便问一下,this.model或this.collection是什么?我知道它们指的是Backbone.Model和Backbone.Collection但它们是如何工作的?我问这个是因为在另一个教程中this.collection和this.

javascript - 在 Meteor 中使用 Jquery $(this)

如何在meteor中使用这个函数?例如,我希望能够单击任何给定的元素并找出它的类是什么。另外,如何获取有关我使用Meteor单击的项目的信息? 最佳答案 假设您在代码中的某处有一个处理事件的模板:Template.tmpl_name.events={'click#logo':function(e){//Insteadofusing$(this),youcando:var$this=$(e.target);//Yourusualcodehere,e.g.:console.log($this.attr('href'));}};

javascript - 如何避免在 Vue 中一直写 this.$store.state.donkey?

我正在学习Vue,我注意到我到处都有或多或少的以下语法。exportdefault{components:{Navigation,View1},computed:{classObject:function(){return{alert:this.$store.state.environment!=="dev",info:this.$store.state.environment==="dev"};}}}一直写出this.$store.state.donkey很痛苦,而且它也降低了可读性。我感觉到我正在以一种不太理想的方式来做这件事。我应该如何引用商店的状态?

javascript - 扩展EventEmitter时如何解析 'this is not defined'?

这个问题在这里已经有了答案:"UncaughtReferenceError:thisisnotdefined"inclassconstructor(1个回答)关闭6年前。以下代码失败:varEventEmitter=require('events');classFooextendsEventEmitter{constructor(){this.name='foo';}print(){this.name='hello';console.log('world');}}varf=newFoo();console.log(f.print());并打印错误this.name='foo';^Ref

javascript - jQuery 如何劫持 "this"?

我只是想知道jQuery是如何劫持Javascript中的“this”关键字的。从我正在阅读的书中:“Javascript权威指南”它指出“this”是一个关键字,你不能像使用标识符那样改变它。现在,假设您在自己的对象构造函数中调用了一些jQuery代码,它如何从您那里劫持它?functionMyObject(){//Atthispoint"this"isreferringtothisobject$("div").each(function(){//Nowthisreferstothecurrentlymatcheddiv});}我唯一的猜测是,由于您正在为jQueryeach()函数