#多種方式實現MVVM,本章先講解基於defineProperty來實現,也就是相似於實現vue的mvvm功能。本系列有3種實現方式,defineProperty(VUE),髒檢查(angular),原生js實現(發佈訂閱者模式)
github地址 查看源碼 https://github.com/honeydlp/mvvm.git
#vue.js 則是採用數據劫持結合發佈者-訂閱者模式的方式,經過`Object.defineProperty()`來劫持各個屬性的`setter`,`getter`,在數據變更時發佈消息給訂閱者,觸發相應的監聽回調。
先看原理圖,再結合代碼vue
![](http://static.javashuo.com/static/loading.gif)
代碼貼圖了,今天用掘金code 老是顯示在一行,因此截圖了,抱歉哈,代碼在github上git
dom結構github
![](http://static.javashuo.com/static/loading.gif)
MVVM調用dom
![](http://static.javashuo.com/static/loading.gif)
MVVM實現mvvm
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)