最近在看一些前端MV框架的实现原理,按照我的理解来看,在模板渲染方式上实现方式有以下几种:
1.基于脏检查的模式,包括熟悉的RegularJS和AngularJS
2.基于defineOwnProperty的getter和setter模式,比如avalon vue1
3.基于Vitual Dom的diff并局部替换真实DOM Tree的模式,大名鼎鼎的React
4.基于*Object.observe(O.o)直接监听数据的变化(下面讨论的重点)
===
在去年的这个时候,Chrome的V8引擎实现了(O.o),被认为对将来的前端框架的一次革新,它更新页面模板性能上提高了很多倍。
相关文章诸如Object.observe() 引爆数据绑定革命,在RegularJS中,其作者(郑海波大神)甚至还在Regular上留下了待更新的部分。regular正是在这种百花齐放的时候产生, 最终在实现上采取了angular的数据更新策略(但是提取了表达式的依赖关系以便在Object.observe正式到来时切换到脏检查+observe的形式)提倡极致的声明式和裸数据操作, 依赖于基于字符串的模板描述结构结合更规范性的类式继承的组件体系来定义数据层的业务逻辑.
RegularJS —— 来自网易的JavaScript MVC框架
当我在 CanIuse 去查询它的实现的范围时,发现只有Chrome的特定版本(45)实现,并在其后的版本删除了这个特性。
然而却尴尬地发现这个特性已经被移除草案了。详见:Object.observe是否被移除ES7草案?
JavaScript upgrade will nix object observation feature这篇文章中介绍了其背景。
===
大概的意思是说情况有所变更,导致其不再适用,随着三年过去,基于数据绑定的组件是很难去匹配(O.o)的,这将会导致前端更混乱的场面。
Object.observe, a planned feature for JavaScript that would allow for direct observation of changes to objects, will be removed from a planned update to the ECMAScript specification under a plan floated this week.
The plan, offered by Object.observe proponent Adam Klein on the ECMAScript discussion archives site, says circumstances surrounding the technology have changed, making it unfeasible.
Object-observe has been slated for inclusion in ECMAScript 7, also known as ECMAScript 2016. Klein wants to withdraw Object.observe from the TC39 committee, which has jurisdiction over ECMAScript, the technical specification underlying JavaScript. He also said he’ll remove support for it from Google’s V8 JavaScript engine.
“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),” Klein said in a blog post. “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,” he said. While data-binding frameworks such as Ember and Angular showed interest, it was difficult to see how they could evolve their existing model to match 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.”
Klein recommends developers who have experimented with Object.observe instead consider a polyfill such as MaxArt2501/object-observe or a wrapper library such as polymer/observe-js, for observing changes in JavaScript data.
(待翻译)