Object.observe將不加入到ES7

先請看 Object.observe 的 APIjavascript

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

它用來監聽對象的變化,當給該對象添加屬性,修改屬性時都會被依次記錄下來html

 

看一個示例java

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

在 chrome 控制檯裏修改 persongit

能夠看到給 person 添加、修改和刪除屬性都沒記錄下來了。這個 person 能夠看出是 MV* 裏的 Model,當數據模型發生變化的時候,經過 Object.observe 的回調就能方便的監聽,通知 View。angularjs

 

這是一個強大的功能,能夠實現不少MV*庫裏的 「雙向綁定」,好比 AngularKnockout。有了它沒必要本身去寫一套觀察者代碼,惋惜ES7最終將它放棄github

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.

 

 

相關:web

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

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

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

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

相關文章
相關標籤/搜索