Tag: 这个

反应:这在事件处理程序中是空的

我有一个LoginForm组件。 我想在提交之前检查, password和password loginName设置。 我试着用这个代码(省略了很多东西): class LoginForm extends Component { constructor() { super(); this.state = { error: "", loginName: "", password: "", remember: true }; } submit(e) { e.preventDefault(); if(!this.state.loginName || !this.state.password) { //this is null this.setState({ error: "Fill in both fields" }); } else { console.log("submitting form"); } } render() { return ( <div […]

在C ++中使用“this”关键字

可能重复: 在C ++中过度使用这个代码的气味 什么时候应该在C ++中使用“this”关键字? 有什么理由使用这个 – > 在C ++中,关键字“this”通常被忽略? 例如: Person::Person(int age) { _age = age; } 而不是: Person::Person(int age) { this->_age = age; }

反应:“this”在组件函数中是未定义的

class PlayerControls extends React.Component { constructor(props) { super(props) this.state = { loopActive: false, shuffleActive: false, } } render() { var shuffleClassName = this.state.toggleActive ? "player-control-icon active" : "player-control-icon" return ( <div className="player-controls"> <FontAwesome className="player-control-icon" name='refresh' onClick={this.onToggleLoop} spin={this.state.loopActive} /> <FontAwesome className={shuffleClassName} name='random' onClick={this.onToggleShuffle} /> </div> ); } onToggleLoop(event) { // "this is undefined??" <— here this.setState({loopActive: […]

在静态函数中使用这个失败

我有这个方法,我想使用$ this,但我得到的是:致命的错误:使用$ this时不在对象上下文中。 我怎样才能使这个工作? public static function userNameAvailibility() { $result = $this->getsomthin(); }

为什么在使用promise的时候,这个在类方法中是未定义的?

我有一个JavaScript类,每个方法返回一个Q诺。 我想知道为什么this是未定义在method2和method3 。 有没有更正确的方法来编写这段代码? function MyClass(opts){ this.options = opts; return this.method1() .then(this.method2) .then(this.method3); } MyClass.prototype.method1 = function(){ // …q stuff… console.log(this.options); // logs "opts" object return deferred.promise; }; MyClass.prototype.method2 = function(method1resolve){ // …q stuff… console.log(this); // logs undefined return deferred.promise; }; MyClass.prototype.method3 = function(method2resolve){ // …q stuff… console.log(this); // logs undefined return deferred.promise; }; 我可以通过使用bind来解决这个问题: […]

在初始化列表中使用“this”指针是否安全?

我有两个具有父子关系的类( Parent类“有一个” Child类),而且Child类有一个指向Parent的指针。 在构buildsubprocess时初始化父指针会很好,如下所示: class Child { public: Child (Parent* parnet_ptr_) : parent_ptr(parent_ptr_) {}; private: Parent* parent_ptr; }; class Parent { public: Parent() : child(this) {}; private: Child child; } 现在,我知道人们build议不要在初始化列表中使用this , C ++ FAQ说我会得到一个编译器警告(顺便说一句,在VS2010,我没有得到警告),但我真的很喜欢这个,然后调用一些设置函数在Parent的构造函数中。 我的问题是: 在创buildChild对象时父指针是否定义良好? 如果是这样的话,那么为什么它被认为是不好的做法,如上所述? 谢谢, 波阿斯 编辑:谢谢Timbo,这确实是一个重复 (嗯,我甚至select了相同的类名)。 所以,让我们得到一些附加价值:如何引用? 是否可以/安全地做到以下几点? : class Child { public: Child (Parent& parnet_ptr_) : parent_ptr(parent_ptr_) {}; […]

在一个类的方法里面打印“this”

我知道这可能是痛苦的基本的,但我有一个艰难的时间包裹着我的头。 class Main { constructor() { requestAnimationFrame(this.update); //fine } update(): void { requestAnimationFrame(this.update); //error, because this is window } } 这似乎是我需要一个代理的情况下,所以可以说使用Jquery class Main { constructor() { this.updateProxy = $.proxy(this.update, this); requestAnimationFrame(this.updateProxy); //fine } updateProxy: () => void update(): void { requestAnimationFrame(this.updateProxy); //fine } } 但是来自Actionscript 3的背景,我不太确定这里发生了什么。 对不起,我不确定Javascript在哪里开始,TypeScript结束。 updateProxy: () => void 而且,我不相信我是这样做的。 我想要的最后一件事是我的类的大部分都有aa()函数,需要使用aProxy()来访问,因为我觉得我写了两次相同的东西? 这是正常的吗?

上下文this和getContext()

this和getContext()之间有什么区别,当我说this我的意思是在一个Activity 。

使用JS .call()方法的原因?

我感兴趣什么是在JS中有call()方法的原因。 它似乎重复了this调用通常的方法。 例如,我有一个call()的代码。 var obj = { objType: "Dog" } f = function(did_what, what) { alert(this.objType + " " + did_what + " " + what); } f.call(obj, "ate", "food"); 产量是“狗吃的食物”。 但是,我可以得到相同的结果分配给对象的function。 var obj = { objType: "Dog" } f = function(did_what, what) { alert(this.objType + " " + did_what + " " + what); […]

std :: shared_ptr这个

我目前正在学习如何使用智能指针。 然而,在做一些实验时,我发现了以下的情况,我找不到一个可喜的解决scheme: 假设你有一个A类的对象,它是B类对象(孩子)的父对象,但两者都应该相互认识: class A; class B; class A { public: void addChild(std::shared_ptr<B> child) { children->push_back(child); // How to do pass the pointer correctly? // child->setParent(this); // wrong // ^^^^ } private: std::list<std::shared_ptr<B>> children; }; class B { public: setParent(std::shared_ptr<A> parent) { this->parent = parent; }; private: std::shared_ptr<A> parent; }; 问题是一个类A的对象如何将一个std::shared_ptr自身( this )传递给它的subprocess呢? 有一些Boost共享指针的解决scheme( 获取一个boost::shared_ptr […]