标签: this

如何在保持可读性的同时使用多个构造函数来删除重复的代码?

int a, b, c;

Constructor()
{
    a = 5;
    b = 10;
    c = 15;
    //do stuff
}
Constructor(int x, int y)
{
    a = x;
    b = y;
    c = 15;
    //do stuff
}
Constructor(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

为了防止重复"东西"和一些任务,我尝试了类似的东西:

int a, b, c;

Constructor(): this(5, 10, 15)
{
}
Constructor(int x, int y): this(x, y, 15)
{
}
Constructor(int x, int y, int z) …
Run Code Online (Sandbox Code Playgroud)

c# parameters constructor code-duplication this

40
推荐指数
2
解决办法
5万
查看次数

React:在事件处理程序中为null

我有一个LoginForm组件.我要检查之前提交,这两个loginNamepassword设置.我试过这个代码(省略了很多东西):

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 className="col-xs-12 col-sm-6 col-md-4">
        <form className="login" onSubmit={this.submit}>
          <button type="submit" className="btn btn-default">Sign in</button>
        </form>
      </div>
    );
  }
}

export default LoginForm;
Run Code Online (Sandbox Code Playgroud)

但是,我TypeError在事件处理程序中得到一个,说this是null.

我该怎么办?

javascript this reactjs

39
推荐指数
2
解决办法
1万
查看次数

从泛型lambda调用`this`成员函数 - clang vs gcc

问题:传递一个普通的lambda (到一个模板函数),它捕获this并调用一个this没有显式的成员函数,this->不能在gcc上编译.如果lambda不是通用的,或者lambda没有被传递给任何其他函数但是就地调用,那么它将编译为withoit this->.在所有情况下,Clang都很酷.

时间又一轮的铛VS GCC.谁是对的?

Wandbox示例


template<typename TF>
void call(TF&& f)
{
    f(1);   
}

struct Example
{        
    void foo(int){ }

    void bar()
    {
        call([this](auto x){ foo(x); });
    }
};

int main()
{
    Example{}.bar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
  • bar()=call([this](auto x){ foo(x); });
    • clang ++ 3.6+ 编译.
    • g ++ 5.2+ 无法编译.

      错误:无法调用成员函数'void Example :: foo(int)'没有对象调用([this](auto x){foo(x);});`


  • bar()=call([this](auto x){ this->foo(x); });
    • clang ++ 3.6+ 编译. …

c++ lambda this language-lawyer c++14

38
推荐指数
1
解决办法
2455
查看次数

在模板化派生类中,为什么我需要在成员函数中使用"this->"限定基类成员名称?

当我调查Qt的源代码时,我发现trolltech的人明确使用this关键字来访问析构函数中的字段.

inline ~QScopedPointer()
{
    T *oldD = this->d;
    Cleanup::cleanup(oldD);
    this->d = 0;
}
Run Code Online (Sandbox Code Playgroud)

那么,这个用法有什么意义呢?有什么好处吗?

编辑:对于那些投票支持关闭此问题的人,我怀疑这种用法适用于某些类继承案例

QScopedPointer类定义的一部分:

template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
class QScopedPointer
Run Code Online (Sandbox Code Playgroud)

c++ qt templates this name-lookup

35
推荐指数
1
解决办法
2660
查看次数

Javascript:我是否需要为对象中的每个变量放置this.var?

在C++中,我最熟悉的语言,通常是一个声明这样的对象:

class foo
{
public:
    int bar;
    int getBar() { return bar; }
}
Run Code Online (Sandbox Code Playgroud)

调用getBar()工作正常(忽略bar可能未初始化的事实).内部的变量bargetBar()类的范围内foo,所以我不需要说,this->bar除非我真的需要说清楚我指的是' bar而不是比如参数.

现在,我正试图在Javascript中开始使用OOP.所以,我查找如何定义类并尝试相同的事情:

function foo()
{
     this.bar = 0;
     this.getBar = function() { return bar; }
}
Run Code Online (Sandbox Code Playgroud)

它给了我bar is undefined.更改barthis.bar解决问题,但为每个变量执行此操作会使我的代码变得非常混乱.这对每个变量都是必要的吗?由于我找不到与此有关的任何问题,这让我觉得我做的事情从根本上是错误的.


编辑:是的,所以,从评论我得到的是,this.bar一个对象的属性,引用不同于bar局部变量的东西.有人可以说,为什么这就是范围和对象,以及是否有另一种方法来定义一个没有必要的对象?

javascript scope this

35
推荐指数
2
解决办法
7800
查看次数

在JavaScript(ES6)的构造函数链中调用由子进程覆盖的父函数

我在下面遇到了JavaScript(ES6)的问题

class A{
  constructor(){
    this.foo();
  }
  foo(){
    console.log("foo in A is called");
  }
}

class B extends A{
  constructor(){
    super();
    this.foo();
  }
  foo(){
    console.log("foo in B is called");
  }
}
Run Code Online (Sandbox Code Playgroud)

我期待的是

foo in A is called
foo in B is called
Run Code Online (Sandbox Code Playgroud)

但事实上确实如此

foo in B is called
foo in B is called
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过简单地添加super.foo()B类的foo函数来解决这个问题

class B extends A{
  constructor(){
    super();
    this.foo();
  }
  foo(){
    super.foo() // add this line
    console.log("foo in B is called");
  }
}
Run Code Online (Sandbox Code Playgroud)

但想象一下类似的情景:

Child必须覆盖父项的功能才能执行一些额外的工作,并阻止外部访问能够访问原始功能.

class B …
Run Code Online (Sandbox Code Playgroud)

javascript constructor class this ecmascript-6

35
推荐指数
1
解决办法
4万
查看次数

如何在JSDoc 3标记中记录对象的属性:@this

@param标签允许性质的文件,如

 /**
  * @param {Object} userInfo Information about the user.
  * @param {String} userInfo.name The name of the user.
  * @param {String} userInfo.email The email of the user.
  */
Run Code Online (Sandbox Code Playgroud)

我如何记录@this标签的属性?

 /**
  * @this {Object} 
  * @param {String} this.name The name of the user.
  * @param {String} this.email The email of the user.
  */
Run Code Online (Sandbox Code Playgroud)

我想知道是否有人在该项目上工作.(文档仍在创建......)

this jsdoc

34
推荐指数
2
解决办法
3万
查看次数

上下文与getContext()的区别

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

android this android-context

33
推荐指数
3
解决办法
4万
查看次数

为什么我们不能在静态方法中使用'this'关键字

class Sub {
    static int y;
    public static void foo() {
         this.y = 10;
    }
}
Run Code Online (Sandbox Code Playgroud)

我理解,它this代表调用该方法的对象,并且静态方法不绑定到任何对象.但在上述情况下,变量y也是静态的.

如果我们可以在类对象上调用静态方法,为什么我们不能允许静态方法来设置类的静态变量.

这个附加约束的目的是什么?

java oop static this

33
推荐指数
3
解决办法
6万
查看次数

CoffeeScript:如何使用胖箭头和这个?

我有一个coffeescript类,它有一些jquery事件监听器.我想使用胖箭=>来避免引用类,但我仍然需要引用通常会使用的元素this.我怎么能同时使用?

class PostForm
    constructor: ->
        $('ul.tabs li').on 'click', =>
            tab = $(this)
            @highlight_tab(tab)
            @set_post_type(tab.attr('data-id'))

    highlight_tab: (tab)->
        tab.addClass 'active'

    set_post_type: (id) ->
        $('#post_type_id').val(id)
Run Code Online (Sandbox Code Playgroud)

this coffeescript arrow-functions

33
推荐指数
2
解决办法
6221
查看次数