const present = {
type: '巧克力',
value: 60,
}
const girl = {
name: '小美',
aboutMe: '...',
age: 24,
career: 'teacher',
fakeAvatar: 'xxxx',
avatar: 'xxxx',
phone: 123456,
presents: [],
bottomValue: 50,
lastPresent: present,
}
const JuejinLovers = new Proxy(girl, {
get: function (girl, key) {
if (baseInfo.indexOf(key) !== -1 && !user.isValidated) {
alert('您还没有完成验证哦')
return
}
if (user.isValidated && privateInfo.indexOf(key) !== -1 && !user.isVIP) {
alert('只有VIP才可以查看该信息哦')
return
}
},
set: function (girl, key, val) {
if (key === 'lastPresent') {
if (val.value < girl.bottomValue) {
alert('sorry,您的礼物被拒收了')
return
}
girl.lastPresent = val
girl.presents = [...girl.presents, val]
}
}
})
class PreLoadImage {
constructor(imgNode) {
this.imgNode = imgNode
}
setSrc (imgUrl) {
this.imgNode.src = imgUrl
}
}
class ProxyImage {
static LOADING_URL = 'xxxxxx'
constructor(targetImage) {
this.targetImage = targetImage
}
setSrc (targetUrl) {
this.targetImage.setSrc(ProxyImage.LOADING_URL)
const virtualImage = new Image()
virtualImage.onload = () => {
this.targetImage.setSrc(targetUrl)
}
virtualImage.src = targetUrl
}
}
const addAll = function () {
console.log('进行了一次新计算')
let result = 0
const len = arguments.length
for (let i = 0; i < len; i++) {
result += arguments[i]
}
return result
}
const proxyAddAll = (function () {
const resultCache = {}
return function () {
const args = Array.prototype.join.call(arguments, ',')
if (args in resultCache) {
return resultCache[args]
}
return resultCache[args] = addAll(...arguments)
}
})()