如何在javascript中使用Object.deliverChangeRecords方法

时间:2014-12-15 03:56:09

标签: javascript object.observe

我了解如何使用Object.observe()Object.getNotifier(obj).notifyObject.getNotifier(obj).performChange,但我如何使用Object.deliverChangeRecords()

1 个答案:

答案 0 :(得分:6)

  

Object.deliverChangeRecords的目的是获得同步传递   听取突变的功能。

http://www.danyow.net/object-deliverchangerecords/

这是一个运行示例,演示了包含和不包含deliverChangeRecords的事件序列:



var output = document.getElementById('output');

function runTest(useDeliver) {
  var obj = {};

  output.textContent = '';
 
  function handleChange(records) {
    output.textContent += 'CHANGE DELIVERED\n';
  }
  
  Object.observe(obj, handleChange);
  
  output.textContent += '1\n';
  
  obj.a = 'b';
  
  output.textContent += '2\n';
  
  if (useDeliver) {
    Object.deliverChangeRecords(handleChange);
  }
  
  output.textContent += '3\n';
  
}

<button onclick="runTest(true)">With deliverChangeRecords</button>
<button onclick="runTest(false)">Without deliverChangeRecords</button>
<pre id="output"></pre>
&#13;
&#13;
&#13;