
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue-MVVM</title>
</head>
<body>
<input type="text" id="input1" value="" oninput="myFun()" />
<input type="text" id="input2" />
</body>
<script>
function myFun() {
o._data.test = document.getElementById('input1').value
}
function cb(val) {
console.log('试图更新啦~~');
document.getElementById('input2').value = val
}
function observer(value) {
if (!value || (typeof value !== 'object')) {
return;
}
Object.keys(value).forEach((key) => {
defineReactive(value, key, value[key]);
})
}
function defineReactive(obj, key, val) {
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function reactiveGetter() {
return val;
},
set: function reactiveSetter(newVal) {
if (newVal === val) return;
cb(newVal);
}
});
}
class Vue {
constructor(options) {
this._data = options.data;
observer(this._data)
}
}
var o = new Vue({
data: {
test: ""
}
})
</script>
</html>