博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Object.observe将不加入到ES7
阅读量:5853 次
发布时间:2019-06-19

本文共 1829 字,大约阅读时间需要 6 分钟。

先请看 Object.observe 的 API

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

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

 

看一个示例

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*库里的 “双向绑定”,比如 ,。有了它不必自己去写一套观察者代码,可惜。

Over three years ago, Rafael Weinstein, Erik Arvidsson, and I set out todesign and implement what we believed to be the primitive underlying thedata-binding system of MDV ("model-driven views"). We prototyped animplementation in a branch of V8, then got agreement from the V8 team tobuild a real version upstream, while pushing Object.observe ("O.o") as apart of the upcoming ES7 standard and working with the Polymer team tobuild their data-binding system on top of O.o.Three years later, the world has changed in a variety of ways. While otherdata-binding frameworks (such as Ember and Angular) showed interest, it wasdifficult to see how they could evolve their existing model to match thatof O.o. Polymer rewrote from the ground up for its 1.0 release, and in thatrebuilding did not utilize O.o. And React's processing model, which triesto avoid the mutable state inherent in data-binding systems, has becomequite popular on the web.After much discussion with the parties involved, I plan to withdraw theObject.observe proposal from TC39 (where it currently sits at stage 2 inthe ES spec process), and hope to remove support from V8 by the end of theyear (the feature is used on 0.0169% of Chrome pageviews, according tochromestatus.com).For developers who have been experimenting with O.o and are seeking atransition path, consider using a polyfill such as or a wrapper library like

 

 

相关:

 

转载地址:http://kasjx.baihongyu.com/

你可能感兴趣的文章
第9章 泛型
查看>>
05 吸收应用-会整理还不够?教你吸收、联想、输出、应用
查看>>
Selenium Web 自动化 - Selenium常用API
查看>>
51Nod 1058: N的阶乘的长度(斯特林公式)
查看>>
变量类型以及传递
查看>>
chrome-解决该扩展程序未列在 Chrome 网上应用店中
查看>>
ASP.NET Session原理及处理方法
查看>>
oracle中order by造成分页错误
查看>>
foxmail配置邮箱pop/IMAP
查看>>
Android 自定义坐标轴控件
查看>>
hdu1114 dp(完全背包)
查看>>
html5 canvas 移动小方块
查看>>
荷兰TAC的需求
查看>>
CSS3阴影 box-shadow的使用和技巧总结
查看>>
CodeChef Sereja and LCM(矩阵快速幂)
查看>>
实习日志(1)2011-12-30
查看>>
XML序列化与反序列化(续)
查看>>
实现简单的时间显示
查看>>
进程体系结构
查看>>
移动端开发适配的2中方案
查看>>