情人节,程序猿如何new好对象-about JavaScript
情人节
在今天还在看这篇文章的各位单身汪们,大家情人节..星期日好.为什么不说情人节?对于单身汪来说这不就是个星期天吗?哦对了.还有就是今天各位单身汪都上班了吧,今天真是一个值得庆祝的日子.一边要受到恩爱狗带来的会心一击,一边受到上班带来的debuff.请大家务必要坚持下去,生活这么的美好.何必要跳楼呢?
对象
找不到女朋友,我们可以找对象啊.new Girl 再给Girl加上各种方法和属性比如
var Girl=function(opai,body){
this.opai=opai
this.body=body
}
Girl.prototype.showOpai=function(){
console.log(this.opai)
}
var myGF=new Girl("D","S");
这样就new了一个欧派为D身材为S的妹纸出来了.什么?一个对象还不够么?你想要有一个模板然后创建出更多的妹纸并且这些妹纸会按照你定义的方法来服♂务你么? 并且大冷天的你不想撸太多代码..那么我觉得你需要有一种特殊的方法来new一个对象
论创建对象的模板方法
首先我们来构造我们想要的女票,并且给女票添加上常用的方法.创建出来的女票会按照以下顺序来服♂务
- 陪你撸代码
- 撸完代码后去吃饭
- 吃完饭后去洗澡
- 洗完澡后…
var GirlTemplate=function(name,oupai,youryanzhi){
this.name=name;
this.oupai=oupai;
this.youryanzhi=youryanzhi;
this.favorability=youryanzhi+10;
}
GirlTemplate.prototype.programmingWithYou=function(){
console.log(this.name+this.oupai+"挨着你的手臂,你感觉到一阵阵柔软的触感")
}
GirlTemplate.prototype.goToEat=function(){
console.log(this.name+"在路上用"+this.oupai+"挨着你的手臂")
}
GirlTemplate.prototype.takeBath=function(){
console.log("在洗澡途中你突然闯入进来 受到了"+(100/this.youryanzhi)+"的伤害 并且增加好感度:"+(this.favorability+this.youryanzhi))
}
GirlTemplate.prototype.paPaPa=function(){
throw new Error("该功能因法律法规暂不开放")
}
GirlTemplate.prototype.init=function(){
this.programmingWithYou()
this.goToEat()
this.takeBath()
this.paPaPa()
}
这样你就创建了一个你女票的模板 那么接下来就是要实例化你女票了
var xiaofang=new GirlTemplate("小芳","d",100)
xiaofang.init()
模板方法
突然..你移民到非洲了…为啥不是欧洲..因为你脸黑啊 哈哈哈哈… 于是papapa这一个功能重新开放了..然后你发现你不满意女朋友的某些举动 想更换一下姿势,但是顺序还是保持不变..你发现你必须要更改女票模板方法才能变动…我仅仅是想更改一个地方的内容.顺序不要动啊喂!于是你开始使用模板方法来创建女票的模板.
var GirlTemplate=function(name,oupai,youryanzhi){
this.name=name;
this.oupai=oupai;
this.youryanzhi=youryanzhi;
this.favorability=youryanzhi+10;
}
GirlTemplate.prototype.programmingWithYou=function(){
throw new Error("创建出来的女票必须要有此方法")
}
GirlTemplate.prototype.goToEat=function(){
throw new Error("创建出来的女票必须要有此方法")
}
GirlTemplate.prototype.takeBath=function(){
throw new Error("创建出来的女票必须要有此方法")
}
GirlTemplate.prototype.paPaPa=function(){
throw new Error("该功能因法律法规暂不开放")
}
GirlTemplate.prototype.init=function(){
this.programmingWithYou()
this.goToEat()
this.takeBath()
this.paPaPa()
}
然后由方法编写权由自己来写.顺序控制权放在女票的模板中
var XiaoHong=function(name,oupai,youryanzhi){
this.name=name;
this.oupai=oupai;
this.youryanzhi=youryanzhi;
this.favorability=youryanzhi+10;
}
XiaoHong.prototype=new GirlTemplate();
XiaoHong.prototype.programmingWithYou=function(){
console.log("陪你撸代码")
}
XiaoHong.prototype.goToEat=function(){
console.log("陪你吃饭")
}
XiaoHong.prototype.takeBath=function(){
console.log("洗澡禁止入内")
}
XiaoHong.prototype.paPaPa=function(){
console.log("非洲可以啪啪啪的")
}
var xiaohong=new XiaoHong("小红","A",100)
xiaohong.init()
这样你就可以自己让自己的女票做爱做的事情了..
hook钩子
突然有一天你回国了..但是你已经在非洲购买了多个女票..你想把这些女票带回国..但是papapa这个功能在国内是行不通的..于是你打算在女票模板中加一个hook来确定是否需要开启papapa功能
var GirlTemplate=function(name,oupai,youryanzhi){
this.name=name;
this.oupai=oupai;
this.youryanzhi=youryanzhi;
this.favorability=youryanzhi+10;
}
GirlTemplate.prototype.programmingWithYou=function(){
throw new Error("创建出来的女票必须要有此方法")
}
GirlTemplate.prototype.goToEat=function(){
throw new Error("创建出来的女票必须要有此方法")
}
GirlTemplate.prototype.takeBath=function(){
throw new Error("创建出来的女票必须要有此方法")
}
GirlTemplate.prototype.paPaPa=function(){
throw new Error("该功能因法律法规暂不开放")
}
GirlTemplate.prototype.openPapapa=function(){
return true//默认开启
}
GirlTemplate.prototype.init=function(){
this.programmingWithYou()
this.goToEat()
this.takeBath()
if(this.openPapapa()){
this.paPaPa()
}
}
然后小薇带回国
var XiaoWei=function(name,oupai,youryanzhi){
this.name=name;
this.oupai=oupai;
this.youryanzhi=youryanzhi;
this.favorability=youryanzhi+10;
}
XiaoWei.prototype=new GirlTemplate();
XiaoWei.prototype.programmingWithYou=function(){
console.log("陪你撸代码")
}
XiaoWei.prototype.goToEat=function(){
console.log("陪你吃饭")
}
XiaoWei.prototype.takeBath=function(){
console.log("洗澡禁止入内")
}
XiaoWei.prototype.paPaPa=function(){
console.log("啪啪啪的")
}
XiaoWei.prototype.openPapapa=function(){
return false
}
var xiaowei=new XiaoWei("小薇","B",100)
xiaowei.init()
用模板方法的好处是 通过增加新的子类就能给系统增加新的功能,并不需要改动抽象父类以及其他子类.
论创建对象的组合模式
慢慢的你发现单纯的4项功能以及满足不了你那邪恶的内心了.于是你打算增加更多的功能.每一项功能又会开启更多的功能.
- 陪我说话以及帮我捶腿
- 切菜以及做饭
- 洗澡以及睡觉
这时候可以用命令模式组合一个宏指令来让女票一键做出所有的事
命令模式的重点在于 接口统一
给女票创建一个宏命令添加器
var GirlMacroCommand=function(){
var command_list=[]
return{
add:function(command){
command_list.push(command)
},
execute:function(){
command_list.forEach(function(command,i){
command.execute()
})
}
}
}
给女票添加1方法
var say={
execute:function(){
console.log("我是你女票hello")
}
}
var hammerLeg={
execute:function(){
console.log("我给你捶腿")
}
}
var macroCommand1=GirlMacroCommand();
macroCommand1.add(say);
macroCommand1.add(hammerLeg);
突然你发现切菜与做饭是同一个类也就是可以连接在一起的 于是
var qieCai={
execute:function(){
console.log("切菜中...")
}
}
var Cooking={
execute:function(){
console.log("做饭中")
}
}
var macroCommand2=GirlMacroCommand();
macroCommand2.add(qieCai);
macroCommand2.add(Cooking);
然后洗澡与睡觉也是可以连接在一起的
var Bathe={
execute:function(){
console.log("洗澡")
}
}
var sleep={
execute:function(){
console.log("睡觉之前不做点什么吗?")
}
}
var macroCommand3=GirlMacroCommand();
macroCommand3.add(Bathe);
macroCommand3.add(sleep);
然后你只需要下达一个指令就可以让你女票乖乖的执行她的三个任务
var startCommand=GirlMacroCommand();
startCommand.add(macroCommand1);
startCommand.add(macroCommand2);
startCommand.add(macroCommand3);
startCommand.execute();
结论
当然 以上是花式创建女票的两种方法.你也可以自己研究如何花式的创建属于自己的女朋友.另外推荐看到此处的单身汪一本书《JavaScript设计模式与开发实践》 其实应该叫做《花式创建女朋友的实践方法》.
此值佳节.看到此处的单身汪赶紧运用以上的两种办法然后创建出自己的女朋友.这样今晚上凌晨就可以发条说说了:”今晚,我在宾馆,new女票用了两种姿势”