<script>
//没有代理时
var shoe=function(name){
this.name=name
}
shoe.prototype.getName=function(){
return this.name
}
var star={
buyShoes:function(shoes){
console.log("买了一双"+shoes.getName())
}
}
star.buyShoes(new shoe("皮鞋"))
//有代理存在时
function Shoes(name){
this.name=name
}
Shoes.prototype.getName=function(){
return this.name
}
//代理
var assistsnt={
buyShoes:function(shoes){
star.buyShoes(shoes.getName())
}
}
var star={
buyShoes:function(name){
console.log("买了一双"+name)
}
}
assistsnt.buyShoes(new Shoes("高跟鞋"))
function Ad(price){
this.price=price
}
Ad.prototype.getPrice=function(){
return this.price
}
var assitant={
init:function(ad){
var money=ad.getPrice()
if(money>300){
this.recevied(money)
}
else{
this.reject()
}
},
recevied:function(price){
star.recevied(price)
},
reject:function(){
star.reject()
}
}
var star={
recevied:function(price){
console.log("接到了广告价位是"+price)
},reject:function(){
console.log("我拒绝了广告")
}
}
assitant.init(new Ad(1000))
</script>
代理模式
最新推荐文章于 2024-07-05 23:58:56 发布