【问题标题】:How do I access class properties using 'self = this'?如何使用“self = this”访问类属性?
【发布时间】:2016-06-10 15:32:59
【问题描述】:

我在从方法中访问类属性时遇到问题。我的代码大致如下:

class MyClass {
    self: MyClass = this;
    someString: string = "Hello world!";

    myMethod() {
        return self.someString;
    }
}

这给了我以下错误:Property 'someString' does not exist on type 'Window'. 我不明白self 怎么可以是Window 类型;毕竟我明确地将它声明为类型MyClass,这不会给我任何错误。

我知道在这种情况下我可以只使用return this.someString,但此代码示例只是对问题的简单演示。

【问题讨论】:

  • 你以前做过这个吗?

标签: javascript oop typescript javascript-objects


【解决方案1】:

没有必要在类级别创建自属性,因为您必须以this.self.whatever 的身份访问它,并且它只是this.whatever 的别名。你的复杂的事情,没有任何好处。

self 仅在闭包内保留对 this 的引用才有意义,
不要到处使用 .call(this)

class Foo {
    data: any[];  //let's assume `data` is populated
    bar(){
        var self = this;
        this.data.forEach(function(v, i){
            //`this` points to the global namespace
            //you have to reference it through `self`
            console.log(i, v, self);
        })

        //or you use lambdas
        this.data.forEach((v,i)=>{
            //then TS takes care of `this`
            //lamdas don't have an own `this`- or `arguments`-object
            //ES6 treats them the same way.
            console.log(v,i,this);
        });
    }
}

OT,只是为了完整:
forEach() 有第二个参数来传递 this-object,这样你就可以实际编写

this.data.forEach(function(v, i){
    console.log(i, v, this);  
}, this);

但这不是这个问题的实际主题

【讨论】:

    【解决方案2】:

    您的代码被转译成如下内容(参见playground)。

    var MyClass = (function () {
        function MyClass() {
            this.self = this;
            this.someString = "Hello world!";
        }
        MyClass.prototype.myMethod = function () {
            return self.someString;
        };
        return MyClass;
    }());
    

    你没有在对象的范围内创建自我。您在myMethod 中所指的self 实际上是Window.self

    为了使错误更明显,请尝试将 self 重命名为其他名称:

    class MyClass {
        moo: MyClass = this;
        someString: string = "Hello world!";
    
        myMethod() {
            return moo.someString;
        }
    }
    

    这会导致正确的错误Cannot find name 'moo'. Did you mean the instance member 'this.moo'?

    如果你想访问你的self 属性,你必须使用this.self

    【讨论】:

      【解决方案3】:

      你不应该在你的例子中使用self

      class MyClass {
          someString: string = "Hello world!";
      
          myMethod() {
              return this.someString;
          }
      }
      

      使用this 访问内部字段

      如果您想尝试使用self 访问,您也应该使用this

      myMethod() {
          return this.self.someString;
      }
      

      【讨论】:

        【解决方案4】:

        我觉得这段代码可以用:-

            "use strict"
        class myClass {
        
        
            constructor(someString) {
                this.someString = someString;
                self = this;
            }
        
        
        
        
            myMethod() {
        
                    return this.someString;
            }
        }
        
         var p1 = new myClass("Hello World");
        
        
        console.log(p1.myMethod());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-24
          • 1970-01-01
          • 1970-01-01
          • 2017-03-04
          • 2016-06-07
          • 1970-01-01
          相关资源
          最近更新 更多