Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 自引用类_Javascript_Class_This - Fatal编程技术网

Javascript 自引用类

Javascript 自引用类,javascript,class,this,Javascript,Class,This,我正在学习JS课程,遇到了一个问题。当我失去这个上下文时,比如在承诺之后,我不知道如何自引用构造函数中设置的类变量 例如: class myClass{ constructor(name) { this.name = name; doSomething().then(doSomethingElse) } doSomething(){ return new Promise((resolve, reject)=>{

我正在学习JS课程,遇到了一个问题。当我失去这个上下文时,比如在承诺之后,我不知道如何自引用构造函数中设置的类变量

例如:

class myClass{
    constructor(name) {
        this.name = name;
        doSomething().then(doSomethingElse)
    }

    doSomething(){
        return new Promise((resolve, reject)=>{
            resolve("myAsyncName")
        })
    }

    doSomethingElse(value){
        this.name = value;
    }
}
在doSomethingElse函数中,我将得到一个错误,该错误未定义或无法在其上设置名称。我曾尝试在构造函数中使用settingself=this,但如果我多次使用该类,它就会失效。我只需要能够引用构造函数中设置的变量。我试着搜索了很多帖子,阅读了大量关于如何使用这个和绑定的文章,但是我找不到在这种情况下需要使用的模式

您需要将doSomethingElse函数绑定到此

i、 e

或者,您可以在中使用箭头函数。然后

i、 e

您需要将doSomethingElse函数绑定到此

i、 e

或者,您可以在中使用箭头函数。然后

i、 e


这是由于调用doSomethingElse的上下文造成的。在您的示例中,doSomethingElse中使用的this将引用then的上下文,而不是您的类

有几种方法可以解决这个问题:

一,。始终将方法绑定到类本身

二,。使用ES6 fat箭头,该箭头在词汇上绑定其上下文

三,。为高级用户使用babel的转换类属性

现在这还不是官方ecmascript规范的一部分,所以我不鼓励使用它。我将假设您正在使用巴贝尔,或者在某个时候将使用巴贝尔。如果没有,那完全没关系

使用此插件,您应该能够编写如下内容:

class myClass{
  constructor(name) {
    this.name = name;
    // no need to change anything here
    doSomething().then(doSomethingElse)
  }

  doSomething(){
    return new Promise((resolve, reject)=>{
        resolve("myAsyncName")
    })
  }

  // notice the fat arrow syntax here. `this` will always be
  // referencing myClass
  doSomethingElse = (value) => {
    this.name = value;
  }
}
我强烈推荐阅读凯尔·辛普森(Kyle Simpson)关于这个主题的《你不知道JS:this&object Prototype》


祝您在学习Javascript的过程中好运

这是由于调用doSomethingElse的上下文造成的。在您的示例中,doSomethingElse中使用的this将引用then的上下文,而不是您的类

有几种方法可以解决这个问题:

一,。始终将方法绑定到类本身

二,。使用ES6 fat箭头,该箭头在词汇上绑定其上下文

三,。为高级用户使用babel的转换类属性

现在这还不是官方ecmascript规范的一部分,所以我不鼓励使用它。我将假设您正在使用巴贝尔,或者在某个时候将使用巴贝尔。如果没有,那完全没关系

使用此插件,您应该能够编写如下内容:

class myClass{
  constructor(name) {
    this.name = name;
    // no need to change anything here
    doSomething().then(doSomethingElse)
  }

  doSomething(){
    return new Promise((resolve, reject)=>{
        resolve("myAsyncName")
    })
  }

  // notice the fat arrow syntax here. `this` will always be
  // referencing myClass
  doSomethingElse = (value) => {
    this.name = value;
  }
}
我强烈推荐阅读凯尔·辛普森(Kyle Simpson)关于这个主题的《你不知道JS:this&object Prototype》

祝您在学习Javascript的过程中好运

constructor(name) {
  this.name = name;

  // binding the `this` context
  this.doSomethingElse = this.doSomethingElse.bind(this);
  // the this in doSomethingElse will always refer the class now
  doSomething().then(doSomethingElse)
}

doSomethingElse() {
constructor(name) {
  this.name = name;
  doSomething().then(() => this.doSomethingElse())
}
class myClass{
  constructor(name) {
    this.name = name;
    // no need to change anything here
    doSomething().then(doSomethingElse)
  }

  doSomething(){
    return new Promise((resolve, reject)=>{
        resolve("myAsyncName")
    })
  }

  // notice the fat arrow syntax here. `this` will always be
  // referencing myClass
  doSomethingElse = (value) => {
    this.name = value;
  }
}