草庐IT

shared_from_this

全部标签

Javascript this 指向 Window 对象

我有以下代码。我希望在我的Firebug控制台上看到“存档”对象,但我看到了Window对象。正常吗?vararchive=function(){}archive.prototype.action={test:function(callback){callback();},test2:function(){console.log(this);}}varoArchive=newarchive();oArchive.action.test(oArchive.action.test2); 最佳答案 oArchive.action.test

javascript - 使用 this/self 引用的 javascript 闭包会导致内存泄漏吗?

根据我对内存泄漏的理解,在闭包中引用范围外的var会导致内存泄漏。但创建“that”var以保留“this”引用并在闭包中使用它也是一种常见的做法,尤其是对于事件。那么,做这样的事情有什么用:SomeObject.prototype.createImage=function(){varthat=this,someImage=newImage();someImage.src='someImage.png';someImage.onload=function(){that.callbackImage(this);}};这不会给项目增加一点漏洞吗? 最佳答案

javascript - Node.js 事件发射器 : How to bind a class context to the event listener and then remove this listener

有没有办法在事件监听器方法中访问类上下文并有可能删除监听器?示例1:import{EventEmitter}from"events";exportdefaultclassEventsExample1{privateemitter:EventEmitter;constructor(privatetext:string){this.emitter=newEventEmitter();this.emitter.addListener("test",this.handleTestEvent);this.emitter.emit("test");}publicdispose(){this.emi

javascript - 使用 express nodejs 获取此错误 auth/operation-not-supported-in-this-environment

我在我的项目中使用Firebase,但在使用google凭据登录时出现此错误auth/operation-not-supported-in-this-environment。.hbs文件代码脚本代码functionloginWithGoogle(event){$.ajax({url:"/session/google/login",type:"POST"}).done(function(data){error=JSON.stringify(data);console.log(error);M.toast({html:error})});}express代码router.post('/se

javascript - 为什么需要匿名函数来使用 setTimeout 保留 "this"

我已经多次使用setTimeout传递函数作为引用,例如setTimeout(someFunction,3000);在某些情况下,为了保留this的值,我不得不事先将其分配给一个变量,但不明白为什么以下内容不起作用:varlogger={log:function(){varthat=this;console.log(that.msg);setTimeout(that.log,3000);},msg:"test"};logger.log();然而,使用匿名函数确实有效:varlogger={log:function(){varthat=this;console.log(that.msg)

javascript - RXJS : moving from of() to scheduled()

我使用of()运算符来创建具有简单值的可观察对象,例如of(navigator.onLine)但我不得不更新RxJS版本,我得到了这个linter警告ofisdeprecated,改用scheduled。这显然不是完全相同的功能,我不应该设置调度程序。你会如何对待它?感谢任何帮助。谢谢。 最佳答案 这个是是一个rxjsbug,它是stillopenclosed。检查here了解更多信息您可以监控此问题并同时使用://tslint:disable-next-line:deprecationof(navigator.onLine);

javascript - Leaflet.Geosearch : get lon/lat from address

在不懂JS的情况下,我被迫在网页上实现了一张map(OSM通过Leaflet)。在这张map上,应该有一个人的实际地址的标记。地址在数据库中保存为字符串。我可以看到一张map,可以给它添加标记,但在那之后,我就迷路了。我已经测试了一些Leaflet-geocoding-plugins,但我必须承认,对于我的实际编程体验而言,它们不够简单。另一个question是关于同样的问题,但我不明白,如何从L.Geosearch的地址获取经/纬度-Leaflet的插件。谁能给我提供一个查找地址的示例(通过OSMN或其他方式,而不是google/bing或其他需要api-key的提供商),将其转换为

javascript - React.JS this.state 未定义

我目前在React.JS中有这个组件,它在数组中显示传递给它的所有图像,onMouseOver它在下面显示一个按钮。我计划使用setState检查变量hover是真还是假,并相应地切换该图像的按钮,但是我不断收到以下错误:UncaughtTypeError:Cannotreadproperty'state'ofundefinedvarImageList=React.createClass({getInitialState:function(){returnthis.state={hover:false};},getComponent:function(index){console.lo

javascript - Jquery - 使用 "this"获取属性

这可能是一件非常简单的事情,但我似乎无法让它工作,我也不确定为什么。我安装了jquery,当我点击它时,我试图获取“this”元素的属性。现在我的代码如下所示:url=$(this).attr("href")当我通过单击链接调用此函数时,它告诉我变量“url”未定义。所以很明显,当我点击链接时,它并没有接收到“this”。我正在尝试传递anchor标记的href以用作我的变量。我忽略了什么?再一次,我知道这很简单,但我似乎无法弄清楚所以感谢您花时间帮助我。谢谢。url="push1";$("a").live("click",function(event){event.preventDe

javascript - 对 Meteor iron-router 中的 this.next() 完全感到困惑

我升级到Meteor1.0,安装了最新的iron-router包,尝试运行我的应用程序并在我的控制台日志中收到这个很好的警告:Routedispatchneverrendered.Didyouforgettocallthis.next()inanonBeforeAction?所以我尝试根据新版本修改我的路由。this.route('gamePage',{path:'/game/:slug/',onBeforeAction:[function(){this.subscribe('singlePlayer',this.params.slug).wait();varsinglePlayer=