let obj = {
//幕后英雄
_txt: "1"
}
// Object.defineProperty 主要是为了自动添加属性
Object.defineProperty(obj, 'txt', {
get: function () {
return obj._txt
},
set: function (newValue) {
obj._txt = newValue
document.getElementById('txt').value = newValue
document.getElementById('show').innerHTML = newValue
}
})
let key = 1
document.addEventListener('keyup', function (e) {
obj.txt = key++
console.log(obj.txt)
})
??先找属性,后找方法
原型链是作用于obj的,作用域链是作用于函数执行的(变量的寻找顺序),this(箭头函数绑定函数外层,如果无函数外层,就是window)
<script type="text/javascript">
"use strict"
let a = {
C: function () {
//定义函数
function D(E,F) {
console.log(this)
console.log(E)
E()
F()
}
//执行函数
D(() => {
console.log(this)
},() => {
console.log(this)
})
}
}
a.C()
</script>
未完成解析的
js的get与set方法不纯粹