代理模式之对象委托
koa
中利用delegates实现了 ctx
的对象委托代理机制。
类似的,在 Vue
中也有代理机制,譬如假设有属性 this.name
,实际上是 this.$data.name
。
js
/*
对象委托机制
target: proto: proto:
{ { {
getName() + name: 'Tom' = name: 'Tom',
getName()
} } }
proto.getName() => target.getName()
*/
const delegates = require('delegates')
function Delegator (proto, target) {
if (!(this instanceof Delegator)) {
return new Delegator(proto, target)
}
this.proto = proto
this.target = target
this.methods = []
}
Delegator.prototype.method = function (methodName) {
this.methods.push(methodName)
const { proto, target } = this
this.proto[methodName] = function (...args) {
return target[methodName].call(proto, ...args)
}
}
const proto = {
name: 'Tom'
}
const target = {
getName () {
return this.name
}
}
Delegator(proto, target).method('getName')
// delegates(proto, target).method('getName')
console.log(proto.getName())