const obj = {
a: '',
b: ''
}
for (const key in obj) {
observerData(obj, key, obj[key]);
}
/**
* 监听对象里面数据是否变化,发生变化告诉操作者
* @param obj 数据对象
* @param key 对象的键
* @param value 对象的值
* **/
function observerData(obj, key, value) {
Object.defineProperty(obj, key, {
get() {
return value;
},
set(newValue) {
if (value !== newValue) {
value = newValue;
render(key, value);
}
}
})
}
function render(key, value) {
console.log("[我的值改变了]", key, value)
}
/**
* JavaScript 中一切都是对象,任何都不例外,
* 对全部值类型应用 Object.prototype.toString.call() 方法结果以下
* **/
console.log(Object.prototype.toString.call(Object)); //[object Function]
console.log(Object.prototype.toString.call({})); //[object Object]
console.log(Object.prototype.toString.call(Array)); //[object Function]
console.log(Object.prototype.toString.call([])); //[object Array]
console.log(Object.prototype.toString.call(String)); //[object Function]
console.log(Object.prototype.toString.call('')); //[object String]
console.log(Object.prototype.toString.call(Boolean)); //[object Function]
console.log(Object.prototype.toString.call(true)); //[object Boolean]
console.log(Object.prototype.toString.call(Number)); //[object Function]
console.log(Object.prototype.toString.call(12)); //[object Number]
console.log(Object.prototype.toString.call(null)); //[object Null]
console.log(Object.prototype.toString.call(Function)); //[object Function]
function a() {}
console.log(Object.prototype.toString.call(a)); //[object Function]