当前位置:网站首页>JS proxy mode
JS proxy mode
2022-06-24 09:51:00 【Time202051】
// The rent paid by the user
class Money {
constructor(money) {
this.money = money
}
}
// tenant Source object
class User {
constructor(name, money, target) {
this.name = name
this.money = money
this.target = target
}
// pay
payMoney() {
const m = new Money(this.money)
this.target.receiveMoney(m.money) // You can use it directly this.money
}
}
// landlord or landlady Target audience
class HouseOwner {
constructor(name) {
this.name = name
}
// Collect money
receiveMoney(money) {
console.log(" landlord or landlady : I received the rent " + money);
}
}
// The mediation Acting as landlord to collect rent
class ProxyHouseOwner {
constructor() {
this.target = new HouseOwner("xx Apartment ")
}
// pay
payMoney(money) {
this.target.receiveMoney(money)
}
// Collect money
receiveMoney(money) {
this.payMoney(money)
}
}
const dl = new ProxyHouseOwner()
const xm = new User(' Xiao Ming ', '1100', dl)
const xh = new User(' Xiaohua ', '2000', dl)
xm.payMoney() // landlord or landlady : I received the rent 1100
xh.payMoney() // landlord or landlady : I received the rent 2000
// star
const star = {
name: ' Stephen Chow ',
age: '55',
phone: 18255556666
}
// agent
const agent = new Proxy(star, {
get: (target, key) => {
if (key === "phone") {
return 17311112222
}
return target[key]
},
set: (target, key, val) => {
if (val < 100000) {
throw new Error(' The price is too low ')
} else {
target[key] = val
}
}
})
console.log(agent.name);
console.log(agent.age);
console.log(agent.phone);
agent.price = 1000000
console.log(agent.price);
边栏推荐
- Baidu AI template for knowledge understanding
- Codeforces Round #392 (Div. 2) D. Ability To Convert
- 二十、处理器调度(RR时间片轮转,MLFQ多级反馈队列,CFS完全公平调度器,优先级翻转;多处理器调度)
- el-table点击添加行样式
- Implementation of simple floating frame in WindowManager
- Reasons for the failure of digital transformation and the way to success
- ThinkPHP5多语言切换项目实战
- JCIM|药物发现中基于AI的蛋白质结构预测:影响和挑战
- 观察者模式
- In depth analysis of Apache bookkeeper series: Part 3 - reading principle
猜你喜欢

e的lnx为什么等于x

R 椭圆随机点产生并画图

Top issue tpami 2022! Behavior recognition based on different data modes: a recent review

如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!

记录一下MySql update会锁定哪些范围的数据

编程题(持续更新)

5分钟,客服聊天处理技巧,炉火纯青

Servlet快速筑基

Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code

indexedDB本地存储,首页优化
随机推荐
LeetCode: 240. 搜索二维矩阵 II
Nlp-d59-nlp game D28 - I think it's OK - stage summary - mentality adjustment
Oracle数据文件头SCN不一致处理方法
最新Windows下Go语言开发环境搭建+GoLand配置
Baidu AI template for knowledge understanding
请问有国内靠谱低手续费的期货开户渠道吗?网上开户安全吗?
新手怎么选择投资理财产品的等级?
Detailed explanation of ThinkPHP 5.0 Model Association
js代理模式
什么情况下应该使用GridFS?
【Eureka注册中心】
Oracle 12c升级至19c后ORA-28000错误
达梦数据库如何定位锁等待问题解决方法
LeetCode: 377. Combined sum IV
编程题(持续更新)
生产者/消费者模型
NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!
Ggplot2 color setting summary
二叉树第一部分
php文件锁