草庐IT

this_needs_servo

全部标签

javascript - this.setState 是否在 react 中返回 promise

我使我的componentWillMount()异步。现在我可以将await与setState一起使用。示例代码如下:componentWillMount=async()=>{const{fetchRooms}=this.propsawaitthis.setState({})fetchRooms()}所以这里的问题是this.setState返回promise因为我可以使用await吗?编辑当我放置await时,它会按顺序运行1,2,3当我删除await时,它会运行1,3,2??componentWillMount=async()=>{const{fetchRooms}=this.pr

javascript - Chrome 扩展,javascript : Why is this firing twice?

我的(测试)Chrome扩展中有一段非常非常简单的代码:functiontest(){alert("Intest!");}chrome.tabs.onUpdated.addListener(function(tabid,changeinfo,tab){varurl=tab.url;if(url!==undefined){test();}});我的问题是,为什么test()会触发两次?更重要的是,如何让它只触发一次? 最佳答案 查看调度事件时的不同状态。我认为,当状态为“正在加载”或状态为“完成”时,它会被分派(dispatch)一次

javascript - 转移 { ..this.props } 但排除某些

是否可以将props向下传输到子组件,其中{..this.props}用于更简洁的语法,但是排除某些props,如className或id? 最佳答案 您可以使用解构来完成这项工作:const{className,id,...newProps}=this.props;//eslint-disable-line//`newProps`variabledoesnotcontain`className`and`id`properties由于此语法目前是ECMAScript提案(Rest/SpreadProperties),您需要转换代码以

javascript - 在 React ComponentDidMount 中访问 this.props

我是React新手,我被困在某个项目上。问题是我在从父组件接收到的this.props中有一个api_url。在这个子组件中,我想使用api_url来使用JSON获取一些数据。在我的父组件中:Repositoriesapi_url={this.state.objs.repos_url}在子组件中,我想要这样的东西:componentDidMount(){$.getJSON(this.props.api_url,function(json){for(vari=0;i所以我需要的是$.getJSON的url部分对应的api_url。有没有办法在componentDidMount中访问thi

javascript - Javascript 函数中 var 和 this 的区别?

vartools={};tools.triangle=function(){varoriginX=0;varoriginY=0;}vartools={};tools.triangle=function(){this.originX=0;this.originY=0;}这两个代码块之间有什么区别吗?抱歉,如果之前有人问过这个问题。 最佳答案 var在tools.triangle中创建一个局部变量。变量originX和originY不能与tools.triangle外部交互。this是指向您正在处理的当前对象的指针。第二个示例可用于通过

javascript - 'this' 关键字在 Javascript 的对象原型(prototype)中返回窗口对象?

我在一个类中有以下功能:MyClass.prototype.myFunction=function(item,args){console.log(this);}此函数是从我无权更改的外部库调用的。当它被调用时,控制台将“this”记录为窗口对象而不是实际的实例化对象。在搜索计算器时,我发现了这句话:thisissetaccordingtohowthemethodiscalled,andnotaccordingtohowthemethodiswritten.Soforobj.method(),thiswillbesettoobjinsideofmethod().Forobj.method

javascript - 为什么这个闭包不能访问 'this' 关键字? -jQuery

我是闭包(和一般的Javscript)的初学者,我找不到关于这段代码中发生的事情的令人满意的解释:functionmyObject(){this.myHello="hello";this.myMethod=do_stuff;}functiondo_stuff(){varmyThis=this;$.get('http://example.com',function(){alert(this.myHello);alert(myThis.myHello);});}varobj=newmyObject;obj.myMethod();它会提示“undefined”然后是“hello”。显然这不应

javascript - 在 Javascript 中,执行深层复制时,由于属性为 "this",如何避免循环?

我有一些库代码在我身上无休止地循环。我不清楚如何在javascript中最好地执行循环检测和避免。也就是说,没有程序化的方法来检查对象是否来自“this”引用,是吗?这是代码。谢谢!setAttrs:function(config){vargo=Kinetic.GlobalObject;varthat=this;//setpropertiesfromconfigif(config!==undefined){functionsetAttrs(obj,c){for(varkeyinc){varval=c[key];/**ifpropertyisanobject,thenaddanempty

javascript - `(this as any)` 在此 typescript 片段中意味着什么?

我遇到了这段代码,但不明白它到底做了什么:publicuploadItem(value:FileItem):void{letindex=this.getIndexOfItem(value);letitem=this.queue[index];lettransport=this.options.isHTML5?'_xhrTransport':'_iframeTransport';item._prepareToUploading();if(this.isUploading){return;}this.isUploading=true;(thisasany)[transport](item)

javascript - 为什么将 `this` 分配给 `self` 并运行 `self.method()` ?

我正在阅读来自mongoose的源代码Collection.prototype.onOpen=function(){varself=this;this.buffer=false;self.doQueue();};我不明白为什么作者将this赋值给self并运行self.doQueue()。为什么不直接运行:this.buffer=false;this.doQueue();我是javascript的新手,感谢您的帮助。 最佳答案 你是对的,在这种情况下,他们可以简单地使用this。使用me或self是为了确保使用正确的this上下文,