1.0.1 • Published 3 years ago

two-data-read v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

let obj = { txt: "", };

/Object.defineProperty(obj, "txt", { //1.被劫持的对象(obj), //2.被劫持的对象的属性 //3.修改被劫持对象属性的 描述符 get: function () { //使用 obj.txt 时调用 get 默认值为 undefined console.log("get val"); }, //在下方 input 事件中调用了 obj.txt,obj.txt 的值赋值为 input 的 value set: function (newVal) { //修改 obj.txt 属性值时调用 set ,newVal 是被赋予的新值 console.log(newVal); //改变的时候 p 标签的内容等于 被赋予的新值 document.querySelector("#p").innerHTML = newVal; }, }); document.querySelector("#input").addEventListener("input", (e) => { obj.txt = e.target.value; }); console.log(document.querySelector("#input").value);