Object.observe将不加入到ES7

先请看 Object.observe 的 API

1
Object.observe(obj, callback[, acceptList])

它用来监听对象的变化,当给该对象添加属性,修改属性时都会被依次记录下来

 

看一个示例

1
2
3
4
5
var person = {}
Object.observe(person, function(arr) {
    var change = arr[0]
    console.log(change.type, change.name, change.oldValue)
})

在 chrome 控制台里修改 person

可以看到给 person 添加、修改和删除属性都没记录下来了。这个 person 可以看出是 MV* 里的 Model,当数据模型发生变化的时候,通过 Object.observe 的回调就能方便的监听,通知 View。

 

这是一个强大的功能,可以实现很多MV*库里的 “双向绑定”,比如 AngularKnockout。有了它不必自己去写一套观察者代码,可惜ES7最终将它放弃

Over three years ago, Rafael Weinstein, Erik Arvidsson, and I set out to
design and implement what we believed to be the primitive underlying the
data-binding system of MDV ("model-driven views"). We prototyped an
implementation in a branch of V8, then got agreement from the V8 team to
build a real version upstream, while pushing Object.observe ("O.o") as a
part of the upcoming ES7 standard and working with the Polymer team to
build their data-binding system on top of O.o.

Three years later, the world has changed in a variety of ways. While other
data-binding frameworks (such as Ember and Angular) showed interest, it was
difficult to see how they could evolve their existing model to match that
of O.o. Polymer rewrote from the ground up for its 1.0 release, and in that
rebuilding did not utilize O.o. And React's processing model, which tries
to avoid the mutable state inherent in data-binding systems, has become
quite popular on the web.

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).

For developers who have been experimenting with O.o and are seeking a
transition path, consider using a polyfill such as
https://github.com/MaxArt2501/object-observe or a wrapper library like
https://github.com/polymer/observe-js.

 

 

相关:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe

https://github.com/polymer/observe-js

https://github.com/MaxArt2501/object-observe

https://mail.mozilla.org/pipermail/es-discuss/2015-November/044684.html

 

posted on   snandy  阅读(1670)  评论(1编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
历史上的今天:
2012-03-04 使用UglifyJS合并/压缩JavaScript
2011-03-04 存储绑定/生存期
2011-03-04 类型绑定
2011-03-04 变量的显示/隐式声明
2011-03-04 JavaScript奇技淫巧之遍历数组
2011-03-04 判断JS对象是否拥有某属性
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示