MVVM
<script src="./mvvm-master/js/mvvm.js"></script>
MVVM源码
function MVVM(options) {
this.$options = options;
this.$options.beforeCreate && this.$options.beforeCreate();
var data = this._data = this.$options.data;
var me = this;
Object.keys(data).forEach(function(key) {
me._proxy(key);
});
this.$options.created && this.$options.created();
observe(data, this);
this.$compile = new Compile(options.el || document.body, this)
}
MVVM.prototype = {
$watch: function(key, cb, options) {
new Watcher(this, key, cb);
},
_proxy: function(key) {
var me = this;
Object.defineProperty(me, key, {
configurable: false,
enumerable: true,
get: function proxyGetter() {
return me._data[key];
},
set: function proxySetter(newVal) {
me._data[key] = newVal;
}
});
}
};