vue响应式原理实现
2019-01-16 本文已影响3人
尤小小
<input type="text" id="input">
<p id="showText"></p>
<script>
var input = document.querySelector('#input')
var showText = document.querySelector('#showText')
var obj = {}
Object.defineProperty(obj, 'something', {
get: function() {
console.log('get')
},
set: function(val) {
showText.innerText = val;
console.log(val)
}
})
input.addEventListener('keyup', function(event) {
obj.something = event.target.value
})
</script>