一、采用valueOf或toString
const a = {
n: 1,
// valueOf: function () {
// return this.n++;
// },
toString: function () {
return this.n++;
}
}
let a = [1, 2, 3, ];
a.toString = a.shift; // 从开头删除一个
二、采用definePropety的get方法
let n = 1;
Object.defineProperty(window, 'a', {
get() {
return n++;
}
})
三、采用Proxy代理
const a = new Proxy({}, {
n: 1,
get: function () {
return () => this.n++;
}
})
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。