1.js 2.html 3. 給vm.a 從新賦值 就能看到 newVal 和oldVal的變化 ...
1.js
/** * @desc 屬性改變監聽,屬性被set時出發watch的方法,類似vue的watch * @author Jason * @study https://www.jianshu.com/p/00502d10ea95 * @data 2018-04-27 * @constructor * @param {object} opts - 構造參數. @default {data:{},watch:{}}; * @argument {object} data - 要綁定的屬性 * @argument {object} watch - 要監聽的屬性的回調 * watch @callback (newVal,oldVal) - 新值與舊值 */ class watcher{ constructor(opts){ this.$data = this.getBaseType(opts.data) === 'Object' ? opts.data : {}; this.$watch = this.getBaseType(opts.watch) === 'Object' ? opts.watch : {}; for(let key in opts.data){ this.setData(key) } } getBaseType(target) { const typeStr = Object.prototype.toString.apply(target); return typeStr.slice(8, -1); } setData(_key){ Object.defineProperty(this,_key,{ get: function () { return this.$data[_key]; }, set : function (val) { const oldVal = this.$data[_key]; if(oldVal === val)return val; this.$data[_key] = val; this.$watch[_key] && typeof this.$watch[_key] === 'function' && ( this.$watch[_key].call(this,val,oldVal) ); return val; }, }); } } // export default watcher;
2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>wathc</title> </head> <body> <script src="./watch.js"></script> <script> let wm = new watcher({ data:{ a: 0, b: 'hello' }, watch:{ a(newVal,oldVal){ console.log(newVal, oldVal); // 111 0 } } }) wm.a = 111 </script> </body> </html>
3. 給vm.a 從新賦值 就能看到 newVal 和oldVal的變化