草庐IT

this_needs_servo

全部标签

javascript - node.js 模块和函数中 "this"的含义

我有一个由require加载的JavaScript文件。//loadedbyrequire()vara=this;//"this"isanemptyobjectthis.anObject={name:"Anobject"};varaFunction=function(){varinnerThis=this;//"this"isnodeglobalobject};aFunction();(function(anyParameter){console.log(anyParameter.anObject);})(this//"this"issamehavinganObject.Not"glo

javascript - 在 React JS 的 this.setState 回调中使用 this.setState?

是否可以在this.setState的回调中调用this.setState?我正在制作一个RoguelikeDungeon并有一个设置,其中在this.setState的回调中使用了一个辅助函数,它再次调用this.setState。我的游戏此时卡住。所以我在React组件中有一个对象,它有一个生成随机二维数组映射的方法:this.Dungeon.Generate();当游戏开始时,我们在componentDidMount()中调用组件中的如下函数:componentDidMount:function(){this.Dungeon.Generate();this.setState({b

JavaScript 本地作用域 : var vs. this

我似乎无法理解JavaScript变量作用域的特定情况。与我发现的其他示例和问题不同,我对嵌套函数的范围界定很感兴趣。我在thisJSFiddle设置了一个示例.相关部分如下:functionMyObject(){varself=this;vara=1;this.b=2;varinnerMethod=function(){//1and2:directreferencelogMessage("a="+a);//a=1//logMessage("b="+b);//Error:bisnotdefined//3and4:usingthislogMessage("this.a="+this.a)

Javascript:Promises + this

这个问题在这里已经有了答案:Howdoesthe"this"keywordwork,andwhenshoulditbeused?(22个答案)关闭6年前。考虑以下代码:foo:function(){varself=this;varp1=p2=someFunctionThatReturnsAPromise();Promise.all([p1,p2]).then(self.bar);}bar:function(promises){varself=this;console.log(self);}输出:undefined但如果我改为执行以下操作:foo:function(){varself=t

javascript - 箭头函数的 this 值

这个问题在这里已经有了答案:MethodsinES6objects:usingarrowfunctions(6个答案)Howdoesthe"this"keywordinJavascriptactwithinanobjectliteral?[duplicate](4个答案)关闭5年前。我正在尝试理解ECMAScript6中的箭头函数。这是我在阅读时遇到的定义:Arrowfunctionshaveimplicitthisbinding,whichmeansthatthevalueofthethisvalueinsideofanarrowfunctionisawaysthesameasthe

javascript - ES6 箭头函数正在改变 Meteor.publish 中 this 的范围

这个问题在这里已经有了答案:Whatdoes"this"refertoinarrowfunctionsinES6?(10个答案)关闭7年前。所以我开始在Meteor中使用ES6,但显然如果你尝试使用带有箭头函数的Meteor.publish语法,this.userId是未定义的,而如果您将它与常规function(){}一起使用,this.userId可以完美运行,我假设是一种分配不同这,到userId但这只是一个猜测,有谁知道到底发生了什么?Meteor.startup(function(){Meteor.publish("Activities",function(){//withf

javascript - 在 javascript 的父闭包中引用 "this"

我想在Javascript中这样做:functionZ(f){f();}functionA(){this.b=function(){Z(function(){this.c()});}this.c=function(){alert('helloworld!');}}varfoo=newA();foo.b();可以这样实现:functionZ(f){f();}functionA(){varself=this;this.b=function(){Z(function(){self.c()});}this.c=function(){alert('helloworld!');}}varfoo=n

javascript - 为什么 `this===window` 给我假的?

我一直在测试以下代码,但Firefox16和Chrome22给出了不同的结果。console.log(this===window);//falseinFirefoxandtrueinChromeconsole.log(this.window===window);//trueinbothFirefoxandChrome(function(){console.log(this===window);//falseinFirefoxandtrueinChromeconsole.log(this.window===window);//trueinbothFirefoxandChrome})();

javascript - 如何从 jQuery 函数访问外部 this?

出于好奇,有没有办法从paint函数访问this.color?functionFoo(color){this.color=color;this.paint=functionpaint(){$("select").each(function(idx,el){$(el).css("background",color);//OK//$(el).css("background",this.color);//this.colorisundefined})}}newFoo("red").paint();谢谢 最佳答案 varthat=this;

javascript - 在 JavaScript 对象中使用 'this' 关键字

我想认为我了解JavaScript,但我今天发现了一些意想不到的事情,我希望有人能向我解释为什么会这样。拿这个代码varanimalData={cow:"cow",sheep:"sheep",getCow:function(){returnthis.cow;},animalList:[{animalId:this.cow,label:"Thisisacow"},{animalId:this.sheep,label:"Thisisasheep"}]};console.log(animalData.getCow());console.log(JSON.stringify(animalDat