time 6m25s
get set
time 10m43s
set代替了writeable
time 12m54s
time 15m58s
var obj = {
get a() {
return 5;
},
set a(val) {
this.a = val;
}
}
// obj.a=10;
/*RangeError: Maximum call stack size exceeded*/
console.log(obj.a);//5
var obj = {
get a() {
return 5;
},
set a(val) {
this.a = val;
}
}
obj.a=10;
/*RangeError: Maximum call stack size exceeded
* 执行set操作,set操作中继续调用set操作,无限循环*/
// console.log(obj.a);//5
time 19m
一般都这样写
var obj = {
_a: 5,
get a() {
return this._a;
},
set a(val) {
this._a = val;
}
}
obj.a = 10;
console.log(obj.a);//10 设置成功
writeable等都是源操作,共有4种,多么,不多。
time 22m23s
var obj = {};
var _a = 5;
Object.defineProperty(obj, "a", {
get() {
return _a;
},
set(val) {
console.log(this);
this._a=val;
/*这样设置还是5,_a是外界的,不是内部的,相当于obj新建了一个_a*/
// _a = val;
}
})
obj.a = 10;
console.log(obj.a);//5
var obj = {};
var _a = 5;
Object.defineProperty(obj, "a", {
get() {
return _a;
},
set(val) {
console.log(this);
this._a=val;
/*这样设置还是5,_a是外界的,不是内部的*/
_a = val;
}
})
obj.a = 10;
console.log(obj.a);//10
time 26m10s
var pipe = function (value) {
var arr = [];
var proxy = new Proxy(
{},
{
get(target, property, receiver) {
if (property === 'get') {
// console.log(arr);
// arr.reduce((pre,item,index)=>{},value,)
return arr.reduce((val, fn) => {
return fn(val);
}, value)
return;
}
;
arr.push(window[property]);
return receiver;
}
}
);
return proxy;
};
var double = n => n * 2;
var pow = n => n * n;
var reverseInt = n =>
n
.toString()
.split("")
.reverse()
.join("");
console.log(pipe(3).double.pow.reverseInt.get)
time 42m39
time 57m43s