草庐IT

this-reference

全部标签

javascript - ECMAScript(ECMA-262 5.1)中的 `base value` 的 `reference` 是什么?

我一直在尝试理解this值是如何在javascript中设置的,并且发现ECMAScript语言规范非常有用。我在读section8.7referencespecificationtype发现ECMAScript中的引用由3个部分组成,basevalue,referencedname,strictreferenceflag了解section11.2.3.我可以根据他们的名字假设什么是referencedname和strictreferenceflag,但我不明白什么是basevalue.文件说basevalue是undefined,String,Boolean,Number和Objec

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

javascript - this, self, window 和 window.self 有什么区别

如果我打开一个空白页面并在javascript控制台中运行以下命令,我会得到相同的结果:>>>thisDOMWindow>>>selfDOMWindow>>>windowDOMWindow>>>window.selfDOMWindow它们指的是什么?...相同的对象还是其他什么? 最佳答案 window是对脚本执行所在的当前浏览器窗口的引用。window.self显然是其自身的自引用。因为这里的self是全局对象window的一个属性,它也可以被访问,就像它本身就是一个“全局”变量一样:只是self.所以最后三个在大多数情况下确实是

JavaScript 对象函数和 `this` 未绑定(bind)并在表达式/括号中返回时

根据返回的this,第1-2行和第4-5行是有意义的。关于第3行,我缺少什么?我认为它会返回类似于第4-5行的window。在这5个中是否还有其他模式可以帮助证明原因?foo={bar:function(){returnthis}}foo.bar()//==>foo(foo.bar)()//==>foo/butwhy?(foo.bar?foo.bar:$.noop)()//==>window(foo.bar||0)()//==>window 最佳答案 分组运算符不会破坏引发方法调用的属性引用。thespec中明确提到了这一点:NOT

javascript - 有没有更好的方法在 React Component 类中绑定(bind) 'this'?

我目前正在开发一个React应用程序,我发现当一个组件类有很多功能时必须绑定(bind)this有点麻烦。例子classFooextendsComponent{constructor(props){super(props);this.function1=this.function1.bind(this);this.function2=this.function2.bind(this);this.function3=this.function3.bind(this);}function1(){...}function2(){...}function3(){...}}有没有更有效的方法来做