ES7中的Object.observe被移除了吗
发布网友
发布时间:2022-04-20 01:42
我来回答
共1个回答
热心网友
时间:2023-07-03 18:42
通过 Object.observe 能直接知道标准对象的变化或者说对该对象的操作,下面是官方的例子:
var records;
function observer(recs) {
records = recs;
}
var obj = { id: 1 };
Object.observe(obj, observer);
obj.a = 'b';
obj.id++;
Object.defineProperty(obj, 'a', { enumerable: false });
delete obj.a;
Object.preventExtensions(obj);
Object.deliverChangeRecords(observer);
assertChangesAre(records, [
{ object: obj, type: 'add', name: 'a' },
{ object: obj, type: 'update', name: 'id', oldValue: 1 },
{ object: obj, type: 'reconfigure', name: 'a' },
{ object: obj, type: 'delete', name: 'a', oldValue: 'b' },
{ object: obj, type: 'preventExtensions', }
]);
pS:现在ES7只是提案而已,并不是正式标准
pS2:没仔细看,那个链接的草案已经不更新了,这个方法是要弃用了
After much discussion with the parties involved, I plan to withdraw
the Object.observe proposal from TC39 (where it currently sits at
stage 2 in the ES spec process), and hope to remove support from V8 by
the end of the year (the feature is used on 0.0169% of Chrome
pageviews, according to chromestatus.com).