4

私は理解しようとしていますObject.getNotifier(object).performChange。概念的には、「マクロ」以上の変更を定義するために設計されていることを理解しています。例から、誰もが参照しているようです

increment: function(amount) {
  var notifier = Object.getNotifier(this);

  notifier.performChange(Thingy.INCREMENT, function() {
    this.a += amount;
    this.b += amount;
  }, this);

  notifier.notify({
    object: this,
    type: Thingy.INCREMENT,
    incremented: amount
  });
}

私が理解していないのはnotifier.performChange、コールバックとしてではなく、直接渡された匿名関数を単に実行することとどう違うのですか? つまり、以下とどのように違うのでしょうか。

increment: function(amount) {
  var notifier = Object.getNotifier(this);

  this.a += amount;
  this.b += amount;

  notifier.notify({
    object: this,
    type: Thingy.INCREMENT,
    incremented: amount
  });
}

最新の仕様でnotifier.performChangeは、次のように通知として発行されるオブジェクトを返す可能性があることを確認しました。

notifier.performChange(Thing.INCREMENT, function() {
    this.a += amount;
    this.b += amount;

    // a notification is issues with this return value,
    // including the type passed to notifier.performChange,
    // and the object underlying notifier. 
    return {incremented: amount};  
});

これにより、元のコードでは次の必要がなくなりますnotifier.notifyが、それでも、これは砂糖以外のものですか、それとも、変更を加えて自分で通知を発行することとの間に機能的な違いはありますか?

4

2 に答える 2

4

多くのテストを1時間行った後、私はついにそれを理解しました。私は同じ質問をしました(何performChangeのために?)、そしてそれを外して電話するという同じ考えもありました

this.a += amount;
this.b += amount;

ただし、ポイントはnotifier.performChange、オブザーバーが各変更を観察しないようにすることです。

私は次のようにテストしていました:

var obj = {
  x: 5,
  y: 10
};

function noti() {
  console.log('noti start');
  var notifier = Object.getNotifier(obj);

  notifier.performChange('ok', function() {
    obj.x++;
    obj.y++;
  });

  notifier.notify({
    type: 'ok',
    oldValue: 5
  });
  console.log('noti end');
};

function noti2() {
  console.log('noti2 start');
  var notifier = Object.getNotifier(obj);

  obj.x++;
  obj.y++;

  notifier.notify({
    type: 'ok',
    oldValue: 5
  });
  console.log('noti2 end');
};

function observer(changes) {
  for (var change of changes) {
    console.log('observer: change =', change, ' newValue=', change.object[change.name]);
  }
};

Object.observe(obj, observer, ['ok', 'update']);

console.log('calling noti2()');
noti2(); //will log the changes of update twice becuase of the x and y property of obj

// add delay manually because observer calls are asynchronous and
// we want to clearly separate the notification function calls in our logs
setTimeout(function() {
  console.log('calling noti()');

  noti(); //will only log the ok type. that's what they mean by big change
          //so everything you do inside the performChange won't be observed
}, 100);

次のコンソール出力が返されます。

calling noti2()
noti2 start
noti2 end
observer: change = Object {type: "update", object: Object, name: "x", oldValue: 5}  newValue= 6
observer: change = Object {type: "update", object: Object, name: "y", oldValue: 10}  newValue= 11
observer: change = Object {object: Object, type: "ok", oldValue: 5}  newValue= undefined

calling noti()
noti start
noti end
observer: change = Object {object: Object, type: "ok", oldValue: 5}  newValue= undefined
于 2014-12-01T02:15:26.493 に答える