草庐IT

this指向

全部标签

javascript - React - TypeError : this. props.courses.map 不是函数

我创建了一个react-redux应用程序。目前它所做的是从服务器(api)加载类(class),并将它们显示到类(class)组件​​。这完美地工作。我正在尝试添加一个功能,您可以在其中通过将类(class)发布到服务器来创建类(class),然后服务器将返回一个成功对象。但是,当我发布到服务器时,出现以下错误(见下文)。我认为这是由于我的connect语句在监听负载类(class)操作。很明显,它认为它应该得到一个列表,而不是一个成功对象。我已经尝试了一些方法来收听类(class)和成功响应,但是为了节省您阅读我所做的所有奇怪事情的时间,我无法让它发挥作用。有谁知道如何解决这个问题

javascript - React 函数未在 "this"中显示,但仍可调用

我对React函数如何绑定(bind)到this感到困惑。importReact,{Component}from'react';classAppextendsComponent{randomFunction(){console.log("HelloWorld")}render(){return({console.log(this)}{console.log(this.randomFunction)});}}exportdefaultApp;您应该在控制台上看到这两个都返回了一些东西,但是randomFunction不存在于之前的this对象中。如下图所示我很想知道这个链接是如何/在哪里

javascript - 如何获取 Javascript 匿名函数的 "this"(作用域)?

假设我得到一个匿名函数,需要对其上下文进行操作,但它是绑定(bind)到“窗口”还是绑定(bind)到未知对象是不同的。如何获取调用匿名函数的对象的引用?编辑,一些代码:varObjectFromOtherLibIAmNotSupposedToknowAbout={foo:function(){//dosomethingon"this"}}varfunctionbar(callback){//hereIwanttogetareferenceto//ObjectFromOtherLibIAmNotSupposedToknowAbout//ifObjectFromOtherLibIAmNo

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 - 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