簡體   English   中英

在JavaScript中裝飾實例方法的問題

[英]Problems decorating an instance method in JavaScript

我在ES6中裝飾實例方法遇到了一個難題。 我在裝飾方法時沒有任何問題,但似乎它仍然堅持使用類的實例的單個狀態。 以下是我正在處理的內容:

class Test {
    init() {
        this.foo = 'bar';
    }

    @decorator
    decoratedMethod() {
        console.log(this.foo); // undefined
    }
}

let test = new Test();
test.init();
test.decoratedMethod();

function decorator(target, name, descriptor) {
     let fn = target[ name ].bind(target, 'a', 'b');
     return fn;
}

我意識到上面的代碼完全正是應該做的,但如果我想訪問foo和其他屬性添加到范圍,我怎么能裝飾decoratedMethod並仍然綁定新的函數屬性?

方法裝飾器在類聲明時運行一次,而不是在實例化類時運行。 這意味着您的示例中的targetTest.prototype ,而不是實例。 所以你的例子基本上是:

class Test {
  init() {
    this.foo = 'bar';
  }

  decoratedMethod() {
    console.log(this.foo); // undefined
  }
}

Test.prototype.decoratedMethod = 
    Test.prototype.decoratedMethod.bind(Test.prototype, 'a', 'b');

這應該清楚你的代碼失敗的原因。 您綁定的對象沒有foo屬性,只有實例。

如果您希望為每個實例處理裝飾器,事情會變得更復雜,並且您需要在創建實例后進行綁定。 一種方法是

function decorator(target, name, descriptor){
  const {value} = descriptor;
  delete descriptor.value;
  delete descriptor.writable;
  descriptor.get = function(){
    // Create an instance of the bound function for the instance.
    // And set an instance property to override the property
    // from the object prototype.
    Object.defineProperty(this, name, {
      enumerable: descriptor.enumerable,
      configurable: descriptor.configurable,
      value: value.bind(this, 'a', 'b'),
    });

    return this[name];
  };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM