//设计模式 单例模式
function Person(){
}
Person.singleInstance=(function(){
var instance=null;
return function(){
if(instance){
return instance;
}else{
instance= new Person();
return instance;
}
}
})()
var p1=Person.singleInstance()
var p2=Person.singleInstance()
p1===p2
//代理模式
undefined
function Person(){
this.send=function(animal){
console.log(animal)
console.log('hi',animal.name,',主人请你吃鱼哦')
}
}
function Dog(){
this.send=function(animal){
console.log('hi',animal.name,',我请你吃鱼')
}
this.sendAgent=function(animal){
var p=new Person(animal)
p.send(animal)
}
}
function Cat(name){
this.name=name;
}
undefined
var p=new Person()
undefined
p.send(new Cat(‘kitty’))
VM3804:4 Cat {name: ‘kitty’}
VM3804:5 hi kitty ,主人请你吃鱼哦
undefined
var dog =new Dog()
undefined
dog.send(new Cat(‘kitty’))
VM3804:11 hi kitty ,我请你吃鱼
undefined
dog.sendAgent(new Cat(‘kitty’))
VM3804:4 Cat {name: ‘kitty’}
VM3804:5 hi kitty ,主人请你吃鱼哦
undefined